Tejalal Choudhary “C Programming from Scratch” Pointers Department of Computer Science & Engg SDBCT, Indore tejalal.choudhary@sdbct.ac.in
Contents What is pointer Printing values using pointer Call by reference Pointer arithmetic array and pointer Structure and pointer
Int a=10; Int b=4; char ch=‘A’ 1 2 3 4 5 6 7 12 13 18 24 19 a … … 00000000 00001010 65529 65524 00000000 00000100 65535 65530 00100000 b ch
Recalling variable What does above declaration mean? int a=10; a is an integer variable Reserves 2 bytes in memory Associate the name a with memory location a will have some address
Address of Operator(&) The & operator is used to access the address of a variable The %u is used to print the address with & int a=10; Following statement will print the address of variable a printf(“Address of a=%u”, &a);
Value at address operator(*) Gives the value stored at a particular address Also known as indirection operator. int a=10; The following statements will print the value of a printf(“Value of a=%d”, a); //10 printf(“Value of a=%d”, *(&a)); //10
What is a pointer ? Declaration: int *p; Pointers are variables that contain addresses Pointers would always contain whole numbers. A pointer when incremented always points to an immediately next location of its type. A pointer is a variable which holds/stores the address of another variable Declaration: int *p; p is a pointer variable which can store address of any integer variable Pointer variable can not store normal values p=3456; // not allowed P=&a; //allowed if a is an integer variable
int i = 3; int *j; int **k ; j=&i; K=&j;
} j = &i ; k = &j ; printf ( "\nAddress of i = %u", &i ) ; int i = 3, *j, **k ; j = &i ; k = &j ; printf ( "\nAddress of i = %u", &i ) ; printf ( "\nAddress of i = %u", j ) ; printf ( "\nAddress of i = %u", *k ) ; printf ( "\nAddress of j = %u", &j ) ; printf ( "\nAddress of j = %u", k ) ; printf ( "\nAddress of k = %u", &k ) ; printf ( "\nValue of j = %u", j ) ; printf ( "\nValue of k = %u", k ) ; printf ( "\nValue of i = %d", i ) ; printf ( "\nValue of i = %d", * ( &i ) ) ; printf ( "\nValue of i = %d", *j ) ; printf ( "\nValue of i = %d", **k ) ; } 65524 65524 65524 65522 65522 65520 65524 65522 3 3 3 3
Call by reference s=sum(&a, &b); Arguments can be passed to functions in one of the two ways: Sending the values of the arguments Sending the addresses of the arguments int a=10, b=20, s; s=sum(&a, &b); Function definition int sum(int *p, int *q) { int r=*p+*q; return r; } Passing the addresses of a & b We are passing addresses, we need pointer variable. p & q are pointer
Pointer arithmetic Addition of two pointers Don’t attempt following operations on pointer Addition of two pointers Multiplication of a pointer with a constant Division of a pointer with a constant All of the above
What will be the output of the program? void main(){ int i = 3, *x ; float j = 1.5, *y ; clrscr(); x = &i ; y = &j ; //suppose address of i=200 and address of j=400 x++ ; y++ ; printf( "\n %u ", x ) ; printf( " %u", y ) ; getch(); } 202 402 200 400 204 408 202 404 202 404
What will be the output of the program? #include<stdio.h> #include<conio.h> void main( ) { int i = 3, *j, **k ; clrscr(); j = &i ;//suppose address of i is 200 k = &j ;//suppose address of j is 400 printf ( "\n%u", j ) ; printf ( ",%u", k ) ; printf ( ",%d", * ( &i ) ) ; printf ( ",%d", **k ) ; getch(); } 200 400 202 400 200 3 400 3 400 3 200 3 200 400 3 3 200 400 3 3