Click here to load reader

POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions

Embed Size (px)

DESCRIPTION

Program to display the address of the variable #include void main () { int var1; printf("Address of var1 variable: %x ", &var1 ); } Address of var1 variable: bff5a400

Citation preview

POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions Introduction to Pointer Variables are stored in the memory Every variable has unique memory location(Address) Address can be accessed using ampersand (&) operator Program to display the address of the variable #include void main () { int var1; printf("Address of var1 variable: %x\n", &var1 ); } Address of var1 variable: bff5a400 Pointers A pointer is a variable whose value is the address of another variable You must declare a pointer before you can use it to store any variable address Declaring Pointer Syntax type *var-name; Eg : int *p1; Program to access the memory location using pointer #include void main () { int var = 20; /* actual variable declaration */ int *ip; /* pointer variable declaration */ ip = &var; /* store address of var in pointer variable*/ printf("Address of var variable: %x\n", &var ); printf("Address stored in ip variable: %x\n", ip ); printf("Value of *ip variable: %d\n", *ip ); } Address of var variable: Address stored in ip variable: Value of *ip variable: 20 Address of var variable: Address stored in ip variable: Value of *ip variable: 20 Program Explanation Memory 20 var int var = 20; 20 var int *ip; ip ip = &var; ip 65558 Other pointer types int *ip; /* pointer to an integer */ double *dp; /* pointer to a double */ float *fp; /* pointer to a float */ char *ch /* pointer to a character */ Null Pointer in C A pointer that is assigned NULL is called a null pointer. It is always a good practice to assign a NULL value to a pointer variable in case you do not have exact address to be assigned. The NULL pointer is a constant with a value of zero Null Pointer Example Program #include void main () { int *ptr = NULL; printf("The value of ptr is : %x\n", ptr ); } The value of ptr is 0 Guess the output of the following program # include void main() { int *p, x=20; p=&x; x= *p+15; printf(%d, x); printf(%d, *p); } Pointer arithmetic int *a; Increment Operator a++ : address will be incremented by 2 (if it is int) (*a)++ : content will be incremented by 1 Example int *a, b=30; /* assume address of b is 2000 */ a=&b; b++ => 2002 *b++ => 31 Pointer arithmetic with Array Example1 #include void main () { int var[ ] = {10, 100, 200}; int i, *ptr; ptr = &var[0]; printf(the value is %d\n, *ptr); ptr++; printf(the value is %d\n, *ptr); printf(the address is %x\n, ptr); } Assume starting address of var[] is 2500 the value is 10 the value is 100 the address is 2502 the value is 10 the value is 100 the address is 2502 Pointer arithmetic with array Example2 #include void main () { int var[ ] = {10, 100, 200}; int i, *ptr; ptr = &var[0]; printf(the value is %d\n, *ptr); (*ptr)++; printf(the value is %d\n, *ptr); printf(the address is %x\n, ptr); } Assume starting address of var[] is 2500 the value is 10 the value is 11 the address is 2500 the value is 10 the value is 11 the address is 2500 Sum of array using pointer Example 3 #include void main() { int a[5]={1,2,3,4,5}; int i,sum=0; int *b; b=a; for(i=0;i