You are on page 1of 9

ESc101: Fundamentals of Computing

2011-12-Monsoon Semester Lecture #21, September 20, 2011

Please switch off your mobile phones.

Announcements

Mid-sem exam copies will be returned in Wednesday Tutorial. Mid sem

Lec-21

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap
Pointers
Variable declaration Memory model and addresses * and & operators Arrays as pointers

Lec-21

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: Pointer
A pointer is a variable that stores the address of another variable. A pointer is denoted by the symbol *
int i; int *p; p = &i; // i is a variable of type int // p is a pointer to some integer // p is now address of i (1 in our example)

The address of a variable is denoted by the symbol & p stores the address of i, i.e., &i *p denotes the content pointed by p, i.e., i.
Lec-21 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 3

Recap: Have we seen pointers before


Arrays are passed as address Array names are essentially pointers
int a [8];

a is a pointer to the first element of the array, i.e., a[0] *a is equivalent to a[0] Since array elements are stored contiguously in memory
(a + i) is a pointer to a[i] *(a + i) is equivalent to a[i] ( ) q [] Notice that pointer arithmetic is different from normal arithmetic
When we add 1 to a, we are actually telling the computer to get the address of the next integer (or whatever type array is of)
Lec-21 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 4

Recap: More on pointers and arrays


When a[i] is used, compiler actually computes the address used of (a + i) and accesses the variable in that address
If the computer has 4-byte integers, then (a+i) would actually translate to a + 4i, where a is the address of a[0] No error checking on array boundaries is done Therefore, a[-2], a [12], etc., become legal in C language (t oug (though may be wrong as per the logic of your program) ay w o g pe t e og c o you p og a )

Pointers can be assigned array names:


int *p; p = a;
Lec-21 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 5

Recap: Swapping two integers


void swap (int *pa, int *pb) { int t = *pa; pa pb; *pa = *pb; *pb = t; } int main ( ) { int x = 3, y = 4; int *a, *b; printf ( x = %d, y = %d\n , x y); (x %d %d\n x, a = &x; b = &y; swap (a, b); printf (x = %d, y = %d\n, x, y); }
Lec-21 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 6

Pointer Operations
Integers can be added to or subtracted from a pointer
Adds (or subtracts) in units of size of type pointed to int *p, *q, i; p = &i; // suppose p is 2001 p++; // p is now 2005 (assuming int is 4 bytes) q = p -2; // q is now 1997

Pointer cannot be multiplied or divided by an integer Pointers can be compared (useful in arrays) A special value NULL can be assigned to a pointer
It signifies that pointer points to nothing It is equivalent to 0
No variable can be allotted memory starting address 0
Lec-21 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 7

Pointer Operations
#include <stdio.h> int main ( ) { int i j * * i t i, j, *p, *q; // Assigning constant to a pointer p = 12345678; // Warning, may result in segmentation fault printf (%d\n, *p); // when you de-reference this pointer // Assigning NULL to a pointer p = NULL NULL; printf (%d\n, *p); // Will certainly result in segmentation fault // Size of pointer on your machine printf (%d\n, sizeof (p)); // Will print 8 on lab machines
Lec-21 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 8

Pointer Operations
// Printing a pointer p = &i; printf (%u\n, p); // Incrementing, Decrementing a pointer I ti D ti i t q = p 1; p++; printf (%u\t%u\n, p, q); // Comparing two pointers printf (%d\n, (p < q)); // Storing values in memory areas pointed to by pointer variables p = &i; q = &j; *p = 7; *q = 8; printf (%d\t%d\t%d\t%d\t\n, (*p), (*q), i, j);
Lec-21 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 9

Pointer Operations
// Assigning a pointer to another pointer g g p p p = q; printf (%d\t%d\n, (*p), (*q)); *p = 4; printf (%d\n, (*q)); // Subtracting a pointer from another pointer p = q + 10; ; printf (%d\n, (q p)); }

Lec-21

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

10

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)
Lec-21

{ printf (%d\t, *p); p++; i++; } printf (\n); } int main ( ) { int a [ ] = {3, -2, 7, 19}; print_array (a, 4); read_array ( , 4); y (a, ); print_array (a, 4); }

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

11

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

Lec-21

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

12

Passing arrays as pointers


#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)
Lec-21

{ printf (%d\t, a[i]); i++; } printf (\n); } int main ( ) { int b [ ] = {3, -2, 7, 19}; int *p = b; p print_p pointer (p, 4); ); print_pointer (p+1, 3); read_pointer (p, 4); print_pointer (p, 4); }
13

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Sizes of variables
Size of a variable can be found out using sizeof function
int *p; a[10]; printf (%d\n, sizeof (int)); printf (%d\n, sizeof (char)); printf (%d\n, sizeof (float)); printf (%d\n, sizeof (double)); printf (%d\n, sizeof (long int)); i f (%d\ i f (l i )) printf (%d\n, sizeof (p)); printf (%d\n, sizeof (a));

Lec-21

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

14

String copy using arrays


void strcpy (char s [ ], char t [ ]) py ( ] { int i = 0; while (t[i] != \0) { s[i] = t[i]; // copy i++; // point to next element } s[i] = \0; // terminate s }

Lec-21

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

15

String copy using pointers


void strcpy (char *s, char *t) py ( ) { while (*t != \0) { *s = *t; s++; t++; } *s = \0; }

// copy // point to next element // point to next element // terminate s

Lec-21

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

16

Any Questions?

Lec-21

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

17