ESC101N Fundamentals of Computing

Similar documents
Number Systems, Scalar Types, and Input and Output

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -VARIABLE AND DATA TYPES- SPRING 2015, SEON-JU AHN, CNU EE

ESC101N: Fundamentals of Computing End-sem st semester

Data Type Fall 2014 Jinkyu Jeong

Programming in C Quick Start! Biostatistics 615 Lecture 4

ESc101: Pointers and Arrays

Pointers as Arguments

First of all, it is a variable, just like other variables you studied

Data Types and Computer Storage Arrays and Pointers. K&R, chapter 5

Introduction to Scientific Computing and Problem Solving

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

Programming in C++ 5. Integral data types

by Pearson Education, Inc. All Rights Reserved.

Internal representation. Bitwise operators

Pointers and Arrays 1

Internal representation. Bitwise operators

Internal representation. Bitwise operators

[0569] p 0318 garbage

PROGRAMMAZIONE I A.A. 2017/2018

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Pointers. Introduction

Pointers and File Handling

Lab 3. Pointers Programming Lab (Using C) XU Silei

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

EM108 Software Development for Engineers

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

Intermediate Programming, Spring 2017*

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

AMCAT Automata Coding Sample Questions And Answers

Subject: Fundamental of Computer Programming 2068

Lecture 03 Bits, Bytes and Data Types

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Types, Variables, and Constants

DECLARAING AND INITIALIZING POINTERS

Principles of C and Memory Management

Arrays and Pointers (part 1)

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

At the end of this module, the student should be able to:

Understanding Pointers

Topics so far. Review. scanf/fscanf. How data is read 1/20/2011. All code handin sare at /afs/andrew/course/15/123/handin

Lesson 7. Reading and Writing a.k.a. Input and Output

Arrays and Strings. Arash Rafiey. September 12, 2017

ARRAYS(II Unit Part II)

Memory, Arrays & Pointers

OBJECT ORIENTED PROGRAMMING

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Arrays and Pointers in C. Alan L. Cox

Fundamentals of Programming Session 20

PROGRAMMAZIONE I A.A. 2017/2018

A Fast Review of C Essentials Part I

Fundamental of Programming (C)

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

Goals of this Lecture

Homework #3 CS2255 Fall 2012

C Pointers. sizeof Returns size of operand in bytes For arrays: size of 1 element * number of elements if sizeof( int ) equals 4 bytes, then

BLM2031 Structured Programming. Zeyneb KURT

CS 137 Part 5. Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors. October 25th, 2017

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

PDS Class Test 2. Room Sections No of students

Fundamentals of Programming

C-LANGUAGE CURRICULAM

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Arrays and Pointers (part 1)

3/13/2012. ESc101: Introduction to Computers and Programming Languages

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Lecture 04 Introduction to pointers

Language comparison. C has pointers. Java has references. C++ has pointers and references

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Homework 1 Introduction to Computational Finance Spring 2019

Programming for Engineers Introduction to C

b. array s first element address c. base address of an array d. all elements of an array e. both b and c 9. An array elements are always stored in a.

Linked Lists. .. and other linked structures. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays

C: How to Program. Week /Mar/05

... Lecture 12. Pointers

Arrays, Pointers and Memory Management

What have we learned about when we learned about function parameters? 1-1

Memory Corruption 101 From Primitives to Exploit

CSE 220: Systems Programming

EECS2031. Modifiers. Data Types. Lecture 2 Data types. signed (unsigned) int long int long long int int may be omitted sizeof()

Fundamentals of Programming Session 8

Why arrays? To group distinct variables of the same type under a single name.

File Handling in C. EECS 2031 Fall October 27, 2014

Lectures 5-6: Introduction to C

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

Transcription:

ESC101N Fundamentals of Computing Arnab Bhattacharya arnabb@iitk.ac.in Indian Institute of Technology, Kanpur http://www.iitk.ac.in/esc101/ 1 st semester, 2010-11 Tue, Wed, Fri 0800-0900 at L7 Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 1 / 18

Sizes of variables The size of a variable can be found out using the sizeof function printf ( %d\n, sizeof ( int )); In this machine char: 1 byte int: 4 bytes float: 4 bytes double: 8 bytes Why are these required? Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 2 / 18

Sizes of variables The size of a variable can be found out using the sizeof function printf ( %d\n, sizeof ( int )); In this machine char: 1 byte int: 4 bytes float: 4 bytes double: 8 bytes Why are these required? To know the limits Type Minimum limit Maximum limit char -128 127 int -2147483648 2147483647 Type Maximum precision Minimum limit Maximum limit float 1.175494E-38-3.403823E38 3.403823E38 double 2.225074E-308-1.798693E308 1.798693E308 Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 2 / 18

Sizes of variables # include <stdio.h> int main () char c; printf (" Size of character = %d bytes \n", sizeof (c)); int i; printf (" Size of int = %d bytes \n", sizeof (i)); float f; printf (" Size of float = %d bytes \n", sizeof (f)); double d; printf (" Size of double = %d bytes \n", sizeof (d)); Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 3 / 18

Limits of variables # include <stdio.h> # include < limits.h> // for limits on integer types # include <float.h> // for limits on floating - point types void main () printf (" char stores values from %d to %d\n", CHAR_MIN, CHAR_MAX ); printf ("\n"); printf (" int stores values from %d to %d\n", INT_MIN, INT_MAX ); printf ("\n"); printf (" Smallest non -zero value of type float is %e\n", FLT_MIN ); printf (" Largest value of type float is %e\n", FLT_MAX ); printf (" Smallest value of type float is %e\n", -FLT_MAX ); printf (" Smallest non -zero addition value of type float is %e\n", FLT_EPSILON ); printf ("\n"); printf (" Smallest non -zero value of type double is %e\n", DBL_MIN ); printf (" Largest value of type double is %e\n", DBL_MAX ); printf (" Smallest value of type double is %e\n", -DBL_MAX ); printf (" Smallest non -zero addition value of type double is %e\n", DBL_EPSILON ); printf ("\n"); Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 4 / 18

Memory model Memory is a list of bytes, each having a particular index called address i c 0 1 4 7... Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 5 / 18

Memory model Memory is a list of bytes, each having a particular index called address i c 0 1 4 7... All variables are stored in memory Depending on the size, contiguous bytes are occupied The address of a variable is the first byte where it is stored Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 5 / 18

Memory model Memory is a list of bytes, each having a particular index called address i c 0 1 All variables are stored in memory 4 7... Depending on the size, contiguous bytes are occupied The address of a variable is the first byte where it is stored int i; char c; Address of i is 1 It occupies bytes 1 through 4 Address of c is 7 It only occupies byte 7 Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 5 / 18

Pointer A pointer is a variable that stores the address of another variable i... p 0 1... 49 Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 6 / 18

Pointer A pointer is a variable that stores the address of another variable i... p 0 1... 49 The type of a pointer depends on the type of the variable it points to A pointer is denoted by the symbol * int i; // i is a variable of type int int * p; // p is a pointer to int The address of a variable is denoted by the symbol & p = &i; // p is now 1 (in figure ) p stores the address of i, i.e., &i *p denotes the content pointed by p, i.e., i Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 6 / 18

Pointer A pointer is a variable that stores the address of another variable i... p 0 1... 49 The type of a pointer depends on the type of the variable it points to A pointer is denoted by the symbol * int i; // i is a variable of type int int * p; // p is a pointer to int The address of a variable is denoted by the symbol & p = &i; // p is now 1 (in figure ) p stores the address of i, i.e., &i *p denotes the content pointed by p, i.e., i The value of a pointer can be printed using %u The size of a pointer can be found using sizeof Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 6 / 18

Pointers # include <stdio.h> int main () int i; int *p; printf (" Size of pointer p is %d\n", sizeof (p)); i = 5; p = &i; printf ("%d\t%d\t%u\t%u\n", i, (*p), &i, p); *p = 7; printf ("%d\t%d\t%u\t%u\n", i, (*p), &i, p); Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 7 / 18

Pointer operations Integers can be added to or subtracted from a pointer Different from simple integer arithmetic Adds (subtracts) in units of the size of the variable pointed to int * p; // suppose p is now 1234 p ++; // p is now 1234 + sizeof ( int ) = 1238 and not 1235 Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 8 / 18

Pointer operations Integers can be added to or subtracted from a pointer Different from simple integer arithmetic Adds (subtracts) in units of the size of the variable pointed to int * p; // suppose p is now 1234 p ++; // p is now 1234 + sizeof ( int ) = 1238 and not 1235 A pointer cannot be added to another pointer Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 8 / 18

Pointer operations Integers can be added to or subtracted from a pointer Different from simple integer arithmetic Adds (subtracts) in units of the size of the variable pointed to int * p; // suppose p is now 1234 p ++; // p is now 1234 + sizeof ( int ) = 1238 and not 1235 A pointer cannot be added to another pointer Pointers cannot be multiplied or divided with integers or other pointers Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 8 / 18

Pointer operations Integers can be added to or subtracted from a pointer Different from simple integer arithmetic Adds (subtracts) in units of the size of the variable pointed to int * p; // suppose p is now 1234 p ++; // p is now 1234 + sizeof ( int ) = 1238 and not 1235 A pointer cannot be added to another pointer Pointers cannot be multiplied or divided with integers or other pointers A pointer can be subtracted from another pointer Useful in arrays Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 8 / 18

Pointer operations Integers can be added to or subtracted from a pointer Different from simple integer arithmetic Adds (subtracts) in units of the size of the variable pointed to int * p; // suppose p is now 1234 p ++; // p is now 1234 + sizeof ( int ) = 1238 and not 1235 A pointer cannot be added to another pointer Pointers cannot be multiplied or divided with integers or other pointers A pointer can be subtracted from another pointer Useful in arrays Pointers can be compared A pointer can be assigned the address of a variable or another pointer Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 8 / 18

Pointer operations Integers can be added to or subtracted from a pointer Different from simple integer arithmetic Adds (subtracts) in units of the size of the variable pointed to int * p; // suppose p is now 1234 p ++; // p is now 1234 + sizeof ( int ) = 1238 and not 1235 A pointer cannot be added to another pointer Pointers cannot be multiplied or divided with integers or other pointers A pointer can be subtracted from another pointer Useful in arrays Pointers can be compared A pointer can be assigned the address of a variable or another pointer A pointer can be assigned a constant integer It is best to avoid such assignments Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 8 / 18

Pointer operations Integers can be added to or subtracted from a pointer Different from simple integer arithmetic Adds (subtracts) in units of the size of the variable pointed to int * p; // suppose p is now 1234 p ++; // p is now 1234 + sizeof ( int ) = 1238 and not 1235 A pointer cannot be added to another pointer Pointers cannot be multiplied or divided with integers or other pointers A pointer can be subtracted from another pointer Useful in arrays Pointers can be compared A pointer can be assigned the address of a variable or another pointer A pointer can be assigned a constant integer It is best to avoid such assignments A special value NULL can be assigned to a pointer It signifies that the pointer points to nothing It is equivalent to 0 Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 8 / 18

Pointer operations I # include <stdio.h> int main () int i, j, *p, *q; printf (" Assigning constant to a pointer \n"); p = 30564378; // warning, may result in segmentation fault printf ("%u\n", p); // printf ("% d\n", (* p)); // may result in segmentation fault printf (" Assigning NULL to a pointer \n"); p = NULL ; printf ("%u\n", p); // printf ("% d\n", (* p)); // error printf (" Printing a pointer \n"); p = &i; printf ("%u\n", p); printf (" Incrementing a pointer \n"); p ++; printf ("%u\t%u\n", p, (p - 3)); printf (" Printing a pointer \n"); q = &j; printf ("%u\n", q); // p = p * 2; // error // q = q / p; // error Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 9 / 18

Pointer operations II printf (" Comparing two pointers \n"); printf ("%d\t%d\n", (p < q), (p > q)); printf (" Assigning address to pointers \n"); p = &i; *p = 3; q = &j; *q = 9; printf ("%u\t%u\t%d\t%d\t%d\t%d\n", p, q, (*p), (*q), i, j); printf (" Assigning a pointer to another pointer \n"); p = q; printf ("%u\t%u\t%d\t%d\n", p, q, (*p), (*q)); *p = 4; printf ("%u\t%u\t%d\t%d\n", p, q, (*p), (*q)); printf (" Subtracting a pointer from another pointer \n"); p = q + 3; printf ("%u\t%u\t%d\t%d\n", p, q, (p - q), (q - p)); printf (" Incrementing the content of a pointer \n"); printf ("%d\t%d\t%d\n", (*p), (*( p + 1)), (*p + 1)); Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 10 / 18

Pointers and arrays Array names are essentially pointers int a [8]; Array elements are stored contiguously in memory a is a pointer to the first element of the array, i.e., a[0] *a is equivalent to a[0] (a + i) is a pointer to a[i] *(a + i) is equivalent to a[i] Remember: (a + i) is actually a + i * sizeof(int) Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 11 / 18

Pointers and arrays Array names are essentially pointers int a [8]; Array elements are stored contiguously in memory a is a pointer to the first element of the array, i.e., a[0] *a is equivalent to a[0] (a + i) is a pointer to a[i] *(a + i) is equivalent to a[i] Remember: (a + i) is actually a + i * sizeof(int) When a[i] is used, compiler actually computes the address of (a + i) and accesses the variable in that address No error checking on array boundaries is done Therefore, a[-2], a[12], etc. become legal as they are computed to possibly valid memory addresses Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 11 / 18

Pointers and arrays Array names are essentially pointers int a [8]; Array elements are stored contiguously in memory a is a pointer to the first element of the array, i.e., a[0] *a is equivalent to a[0] (a + i) is a pointer to a[i] *(a + i) is equivalent to a[i] Remember: (a + i) is actually a + i * sizeof(int) When a[i] is used, compiler actually computes the address of (a + i) and accesses the variable in that address No error checking on array boundaries is done Therefore, a[-2], a[12], etc. become legal as they are computed to possibly valid memory addresses Pointers can be assigned array names int *p; p = a; Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 11 / 18

Passing pointers to functions Pointers can be passed to functions just like any other variable Function header must indicate that the parameters are pointers int swap ( int *pa, int *pb) int t = *pa; *pa = *pb; *pb = t; Important: If content of a pointer is changed inside the function, the change is permanent, i.e., visible even outside the function Reason: When a pointer is passed as an argument, a copy of the pointer is available inside the function However, the copy still points to the same address Hence, changing the content of this address changes the content of the original address Similar to the situation when pointers p and q are same, i.e., p = q; and *p is changed; *q changes automatically Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 12 / 18

Passing pointers to functions Pointers can be passed to functions just like any other variable Function header must indicate that the parameters are pointers int swap ( int *pa, int *pb) int t = *pa; *pa = *pb; *pb = t; Important: If content of a pointer is changed inside the function, the change is permanent, i.e., visible even outside the function Reason: When a pointer is passed as an argument, a copy of the pointer is available inside the function However, the copy still points to the same address Hence, changing the content of this address changes the content of the original address Similar to the situation when pointers p and q are same, i.e., p = q; and *p is changed; *q changes automatically Other rules of parameter passing are followed Change in pointer itself is temporary Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 12 / 18

Swapping two integers # include <stdio.h> void swap ( int *pa, int *pb) int t = *pa; *pa = *pb; *pb = t; int main () int x = 3, y = 4; int *a, *b; printf ("x = %d, y = %d\n", x, y); // swap (&x, &y); // equivalent to the next three lines a = &x; b = &y; swap (a, b); printf ("x = %d, y = %d\n", x, y); x y... a b... pa pb... 0 1 5... 25 29... 49 53... Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 13 / 18

scanf and printf scanf requires passing a pointer Reason: scanf requires to change the content of the variable to what has been typed using the keyboard Therefore, the address of the variable is passed This is also why scanf requires & with the variable scanf ( %d, &i); Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 14 / 18

scanf and printf scanf requires passing a pointer Reason: scanf requires to change the content of the variable to what has been typed using the keyboard Therefore, the address of the variable is passed This is also why scanf requires & with the variable scanf ( %d, &i); printf does not require passing a pointer Reason: printf does not require to change the content of the variable; it just needs to print it Therefore, only the variable is passed This is also why printf does not require & with the variable printf ( %d, i); Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 14 / 18

scanf and printf scanf requires passing a pointer Reason: scanf requires to change the content of the variable to what has been typed using the keyboard Therefore, the address of the variable is passed This is also why scanf requires & with the variable scanf ( %d, &i); printf does not require passing a pointer Reason: printf does not require to change the content of the variable; it just needs to print it Therefore, only the variable is passed This is also why printf does not require & with the variable printf ( %d, i); General rule: If a variable needs to be changed in a function, pass a pointer that holds the address of the variable and change the contents of the pointer Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 14 / 18

Passing arrays as pointers Since array names are pointers, arrays can be passed where pointers are required To access the successive elements of the array, the pointer can be incremented Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 15 / 18

Passing arrays as pointers # include <stdio.h> void read_array ( int *p, int size ) int i = 0; while (i < size ) scanf ("%d", p); p ++; i ++; void print_array ( int *p, int size ) int i = 0; while (i < size ) printf ("%d\t", *p); p ++; i ++; printf ("\n"); int main () int a[] = 3, -2, 7, 19; print_array (a, 4); read_array (a, 4); print_array (a, 4); Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 16 / 18

Passing pointers as arrays Pointers can also be passed where arrays are required The pointer must point to the correct array address Pointer may point to the middle of the array, and operations may start from there Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 17 / 18

Passing pointers as arrays # include <stdio.h> void read_pointer ( int a[], int size ) int i = 0; while (i < size ) scanf ("%d", &a[i]); i ++; void print_pointer ( int a[], int size ) int i = 0; while (i < size ) printf ("%d\t", a[i]); i ++; printf ("\n"); int main () int b[] = 3, -2, 7, 19; int *p = b; print_pointer (p, 4); print_pointer (p + 1, 3); read_pointer (p, 4); print_pointer (p, 4); Arnab Bhattacharya (arnabb@iitk.ac.in) ESC101N 2010 I 18 / 18