Pointer in C Presented By: Pushpendra K. Rajput Assistant Professor 1
Introduction The Pointer is a Variable which holds the Address of the other Variable in same memory. Such as Arrays, structures, and Functions that are used in program. It contains only the Memory Location of the variable rather than its content.
Pointers Pointer is a variable that contains address of another variable. Consider the declaration: int i=3; main() 625524 { int i=3; printf( \naddress of i=%u,&i); printf( \nvalue of i=%d,i); } i 3 Format specifier for unsigned int Location name Value at location Location number Address of
EXAMPLE FOR POINTER: int X = 547; int *ptr; ptr = &X; Veriable name Contents Location X 547 4000 ptr 4000 4036
Operators used with Pointers 1.The address operator & [ampersand] An address operator gives the address of the variable. 2.The indirection operator * [asterisk] The indirection operator gives the value of the variable that the pointer is pointing to.
POINTER DECLARATION In c Every Variable must be Declared its type,since pointer variables contain address of separate Data type, They must be Declared before use them the declaration of pointer variable takes the following form syntax: data _type * pt_name EXAMPLE: 1. int * p; / * integer pointer */ 2. float *p; /* float pointer */ HERE: 1. Declares the variable p as a pointer variable that points to an integer data type 2. Declares the variable p as a pointer variable that points to an float data type
Address of i=65825 value of i=3 void main() { } int i=3; printf( \naddress of i=%u,&i); printf( \nvalue of i=%d,i); printf( \nvalue of i=%d,*(&i)); Address of i=24524 value of i=3 value of i=3 i 3 625524 Value at address Location name Value at location Location number
j=&i; i 3 65258 j 65258 32525 But, we need to declare int *j; means value at address contained in j is an int 8
In Detail We access the value of the variable by help of pointer this is done by using another unary operator * (asterisk) usually known as Indirection operator consider following statements int quantity, *p,n; //Declare two integer and one pointer variable quantity = 179; p = & quantity; // Assign 179 to quantity // Save address of quantity into p n = quantity; n = *p; n = *(&quantity) //Assign value of quantity to n
EXPLANATION: The First line declares quantity & n as integer variable and p as pointer variable The second line Assigns value 179 to variable quantity The third line assigns address of variable quantity to the pointer variable p The forth line contains * operator in this case *p returns value of variable quantity Now value of n would be 179 p = & quantity; n = * p; n = * & quantity; n = quantity; These are equal
Main() { int i=3,*j, **k; j=&i; k=&j; i 3 65258 65258 32525 j Illustration 32525 65522 printf( \n %u,&i); 65258 Address of i Value at j printf( \n %u,j); printf( \n %u,*k); printf( \n %u,&j); printf( \n %u,k); printf( \n %u,&k); printf( \n %u,j); printf( \n %u,k); printf( \n %d,i); printf( \n %d, *(&i)); k printf( \n %d, *j); 11 printf( \n %d, **K);
An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 5 j int integer variable 10 12
An Illustration int i = 5, j = 10; int *ptr; /* declare a pointer-to-integer variable */ int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 5 j int integer variable 10 ptr int * integer pointer variable 13
An Illustration int i = 5, j = 10; int *ptr; int **pptr; /* declare a pointer-to-pointer-to-integer variable */ ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 5 j int integer variable 10 ptr pptr int * integer pointer variable int ** integer pointer pointer variable Double Indirection 14
An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; /* store address-of i to ptr */ pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 5 j int integer variable 10 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable 15
An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; /* store address-of ptr to pptr */ *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 5 j int integer variable 10 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr 16
An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 3 j int integer variable 10 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr *ptr int de-reference of ptr 3 17
An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 7 j int integer variable 10 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable **pptr int de-reference of dereference of pptr address of ptr 7 18
An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 7 j int integer variable 10 ptr int * integer pointer variable address of j pptr int ** integer pointer pointer variable address of ptr *ptr int de-reference of ptr 10 19
An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 7 j int integer variable 9 ptr int * integer pointer variable address of j pptr int ** integer pointer pointer variable **pptr int de-reference of dereference of pptr address of ptr 9 20
An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 7 j int integer variable 9 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr *pptr int * de-reference of pptr value of ptr (address of i) 21
An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable -2 j int integer variable 9 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr *ptr int de-reference of ptr -2 22
Pointers Pointer Arithmetic
Pointer Expressions and Pointer Arithmetic Only Two Arithmetic operations can be performed on pointers : addition and subtraction Increment/decrement pointer (++ or --) Example: ++vptr, vptr++, --vptr, vptr-- Add an integer to a pointer( + or +=, - or -=) Example: vptr=vptr+3, vptr+=3, vptr=vptr-3, vptr-=3 Pointers may be subtracted from each other Operations meaningless unless performed on an array
Example Let P1 be an integer pointer with a current value of 2000. Assume ints are 2 byte long. Then take an expression P1++; Now P1 contains 2002 not 2001. Reason is that each time P1 is incremented it will point to the next integer. The same is true for decrement Now P1 contains 2000 P1- -;
Example Five elements (float) array on machine with 4 byte floats vptr points to first element v[ 0 ]whose address location is 3000 (vptr = 3000) vptr += 2; Or vptr = vptr+2; // sets vptr to 3008 vptr points to v[ 2 ] (incremented by 2), but the machine has 4 byte float, so it points to address 3008 pointer variable vptr location 3000 3004 3008 3012 3016 v[0] v[1] v[2] v[3] v[4]
Char *Ch = &I; Int *In = &J; Example //assume(3000) //assume(3000) Ch Ch+1 Ch+2 Ch+3 Ch+4 Ch+5 3000 3001 3002 3003 3004 3005 Memory In In+1 In+2
Rule An integer quantity can be added to or subtracted from a pointer variable The amount of increment depends on the type of the pointer variable pv+n pv + n*sizeof(variable type that pointer point to) pv-n pv - n*sizeof(variable type that pointer point to)
Example: (double pointer) Assume long (long integer) is 4 bytes, and pointer variable is 2 bytes. long a[10]={5,10,15, }; long *pa, **ppa; int i=5; pa = &a[0]; Expression Value Note 800 700 ppa ppa=&pa; 700 640 pa Variable Address Value a 640 5 644 10 648 15 640 644 648 652 656 660 a 5 10 15 pa 700 640 ppa 800 700 pa+1 644 640+1*4 pa+3 652 640+3*4 pa+i 660 640+i*4 ppa+1 702 700+1*2 ppa+i 710 700+i*2 *pa+1 6 5+1 *(pa+1) 10 a[1]=pa[1]=*(a+1) pa[2] 15 648 *ppa 640 value of pa *ppa+1 644 pa+1 *(ppa+1) invalid *(702) **ppa+1 6 a[0]+1 = 5+1 *(*ppa+1) 10 *(pa+1)=*(640+1*4)
Subtracting pointers One pointer variable can be subtracted from another provided both variables point to elements of the same array. The resulting value indicates the number of elements separating the corresponding array elements. Example: #include<stdio.h> #include<conio.h> void main() { } int arr[] = {10,20,30,40,50,60,70}; int *i, *j; i = &arr[1]; j = &arr[5]; printf( %d %d, j-i, *j-*i); getch(); Output: 4 40 Suppose array begins at location 65502, then the elements would be present at locations 65504 and 65512 respectively. j-i would print a value 4 and not 8. this is because j and i are pointing to locations that are 4 integer apart
Pointer comparison Pointer comparison ( <, ==, > ) See which pointer points to the higher numbered array element Also, see if a pointer points to 0
Pointer assignment Pointers of the same type can be assigned to each other If not the same type, a cast operator must be used Exception: pointer to void (type void *) Generic pointer, represents any type No casting needed to convert a pointer to void pointer void pointers cannot be dereference
Arrays and pointers are closely related` Array name like a constant pointer Pointers can do array subscripting operations Example: Declare an array b[ 5 ] and a pointer bptr bptr = b; // To set them equal to one another // The array name (b) is actually the address of first element of the array bptr = &b[ 0 ]; // Explicitly assigns bptr to address of first element of b To access element b[ 3 ]: x=*( bptr + 3 ) // Where 3 is the offset. Called pointer/offset notation x=bptr[ 3 ] // Called pointer/subscript notation x=*( b + 3 ) // bptr[ 3 ] same as b[ 3 ] // Performing pointer arithmetic on the array itself
Pointers and Arrays Strong relation between pointers and arrays Pointers and arrays can be used interchangeably. The array name is equivalent to the address of the first element in the array Example: int a[10]; int *pa; pa = &a[0]; /* is equivalent to pa = a */ So, a[1] *(pa+1) pa[1] *(a+1) &a[1] pa+1 a+1 a[i] *(pa+i) pa[i] *(a+i) &a[i] pa+i a+i a[i]+=5 *(pa+i)+=5 pa[i]+=5 Example: f(int s[]) { } f(int *s) { }
Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; ptr -=1; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable)? a[1] float float array element (variable)? a[2] float float array element (variable)? a[3] float float array element (variable)? ptr float * float pointer variable *ptr float de-reference of float pointer variable? 35
Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; ptr -=1; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable)? a[1] float float array element (variable)? a[2] float float array element (variable)? a[3] float float array element (variable)? ptr float * float pointer variable address of a[2] *ptr float de-reference of float pointer variable? 36
Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; ptr -=1; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable)? a[1] float float array element (variable)? a[2] float float array element (variable) 3.14 a[3] float float array element (variable)? ptr float * float pointer variable address of a[2] *ptr float de-reference of float pointer variable 3.14 37
Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; ptr -=1; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable)? a[1] float float array element (variable)? a[2] float float array element (variable) 3.14 a[3] flot float array element (variable)? ptr float * float pointer variable address of a[3] *ptr float de-reference of float pointer variable? 38
Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable)? a[1] float float array element (variable)? a[2] float float array element (variable) 3.14 a[3] float float array element (variable) 9.0 ptr *ptr float * float pointer variable float de-reference of float pointer variable address of a[3] 9.0 39
Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable)? a[1] float float array element (variable)? a[2] float float array element (variable) 3.14 a[3] float float array element (variable) 9.0 ptr *ptr float * float pointer variable float de-reference of float pointer variable address of a[0]? 40
Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; ptr -=1; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) 6.0 a[1] float float array element (variable)? a[2] float float array element (variable) 3.14 a[3] float float array element (variable) 9.0 ptr float * float pointer variable address of a[0] *ptr float de-reference of float pointer variable 6.0 41
Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; ptr -=1; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) 6.0 a[1] float float array element (variable)? a[2] float float array element (variable) 3.14 a[3] float float array element (variable) 9.0 ptr float * float pointer variable addres s of a[2] *ptr float de-reference of float pointer variable 3.14 42
Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; ptr -=1; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) 6.0 a[1] float float array element (variable)? a[2] float float array element (variable) 7.0 a[3] float float array element (variable) 9.0 ptr *ptr float * float pointer variable float de-reference of float pointer variable address of a[2] 7.0 43
Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; ptr -=1; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) 6.0 a[1] float float array element (variable)? a[2] float float array element (variable) 7.0 a[3] float float array element (variable) 9.0 ptr *ptr float * float pointer variable float de-reference of float pointer variable address of a[1] 7.0 44
Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; ptr -=1; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) 6.0 a[1] float float array element (variable) 7.0 a[2] float float array element (variable) 7.0 a[3] float float array element (variable) 9.0 ptr *ptr float * float pointer variable float de-reference of float pointer variable address of a[1] 7.0 45
Example: (double pointer) Assume long (long integer) is 4 bytes, and pointer variable is 2 bytes. long a[10]={5,10,15, }; long *pa, **ppa; int i=5; pa = &a[0]; Expression Value Note 800 700 ppa ppa=&pa; 700 640 pa Variable Address Value a 640 5 644 10 648 15 640 644 648 652 656 660 a 5 10 15 pa 700 640 ppa 800 700 pa+1 644 640+1*4 pa+3 652 640+3*4 pa+i 660 640+i*4 ppa+1 702 700+1*2 ppa+i 710 700+i*2 *pa+1 6 5+1 *(pa+1) 10 a[1]=pa[1]=*(a+1) pa[2] 15 648 *ppa 640 value of pa *ppa+1 644 pa+1 *(ppa+1) invalid *(702) **ppa+1 6 a[0]+1 = 5+1 *(*ppa+1) 10 *(pa+1)=*(640+1*4)
Arrays of Pointers Arrays can contain pointers For example: an array of strings char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"}; Strings are pointers to the first character char * each element of suit is a pointer to a char The strings are not actually stored in the array suit, only pointers to the strings are stored suit[0] H e a r t s \0 suit[1] suit[2] suit[3] D i a m o n d s \0 C l u b s \0 S p a d e s \0 suit array has a fixed size, but strings can be of any size
Summary A pointer may be incremented or decremented An integer may be added to or subtracted from a pointer. Pointer variables may be subtracted from one another. Pointer variables can be used in comparisons, but usually only in a comparison to NULL.
Summary Do not attempt the following operations on pointers they would never work out; Addition of two pointers Multiplication of a pointer with a constant Division of a pointer with a constant
Pros and Cons Advantages of Pointers Pointer reduces the length and complexity of a program. Pointers are more efficient in handling the data tables. Pointers increase the execution speed. Pointers are closely associated with arrays and provide an alternate way to access individual array elements. Function cannot return more than one value. But when the same function can modify many pointer variables and function as if it is returning more than one variable.
Pros and Cons Disadvantages of Pointers If sufficient memory is not available during runtime for the storage of pointers, the program may crash (least possible) If the programmer is not careful and consistent with the use of pointers, the program may crash (very possible)
Thank You!!