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<5;i++) { sum=sum + *b; b++; } printf("\n The Sum is %d",sum); } The Sum is 15
Pointer and Functions Call by value Call by Reference Returning pointers
Call by value # include int add(int, int); void main() { int no1,no2, sum; printf(“Enter two numbers\t”); scanf(“%d%d”,&no1,&no2); sum=add(no1,no2); printf(“the sum is %d \t”, sum); } int add(int x, int y) { int s; s=x+y; return(s); } Call by Value
Call by Reference – Example(swapping of two numbers) # include void swap(int*, int*); void main() { int no1=5, no2=8; printf(“values before swapping %d\t %d\n”, no1,no2); swap(&no1,&no2); printf(“values after swapping %d\t %d”, no1,no2); } void swap(int *x, int *y) { int t; t= *x; *x=*y; *y=t; } Call by Reference values before swapping 58 values after swapping 85 values before swapping 58 values after swapping 85
Dynamic memory allocation Static Memory Allocation Allocating memory to variable during compile time of your program Eg. : int a[20]; Disadvantage : Wastage of Memory Space Dynamic Memory allocation Allocating memory to variables during run time of your program Advantage : No wastage of Memory Space
Allocating memory during run time # include void main() { int n, *a,k=1; printf(“Enter no. of values “); scanf(“%d”, &n); a= (int *)malloc(n*sizeof(int)); /* malloc(n*2) */ for(i=0;i<n;i++) { *a=k; a++; k++; }