Pointers in C language are variable that stores/point the address to another variable. And the pointers in C is used to allocate memory dynamically i.e. run time. Pointers will allow the passing of arrays and strings to functions more effectively.
Syntax: data_type *var_name;
Pointers in C with Examples
- Pointers reduce the length and complexity of a program.
- And also increase the processing speed
- These make possible to return more than one value from the functions.
- The size of any pointer is 2byte(for a 16bit compiler)
- Always pointers are initialized to null. i.e int*p = null.
- While the value of the pointers are 0
- & symbol is used to get the address of the variable.
- * symbol is used to get the value of the variable, that the pointer is pointing to.
- The content of a pointer is always a whole number i.e. address.
Example
#include <stdio.h> int main(){ int *ptr, q; q=50; ptr=&q;/* address of q is assigned to ptr */ printf("%d", *ptr);/*display q's value using ptr variable */ return 0; }
Output: 50
Example 2
#include<stdio.h> int 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 );/* address stored in pointer variable */ printf("Value of *ip variable: %d\n", *ip ); /* access the value using the pointer */ return 0;}
pointer to pointer
A pointer can point to another pointer i.e they can store the address of others are known as “double pointer” or “pointer to pointer”. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value.
Example
#include<stdio.h> int main() { int var; int *ptr; int **pptr; var = 2000; /*take the address of var */ ptr = &var; /*take the address of ptr using address of operator & */ pptr = &ptr; /*take the value using pptr */ printf("Value of var = %d\n", var ); printf("Value available at *ptr = %d\n", *ptr ); printf("Value available at **pptr = %d\n", **pptr); return 0; }
Output
Value of var = 2000
Value available at *ptr = 2000
Value available at **pptr = 2000
Passing pointers to functions
Pointers can also be passed as an argument to a function. With this, they can be called as a reference as well as an array can be passed as a reference.
Example
#include<stdio.h> #include<time.h> void getSeconds(unsigned long *par); int main () { unsigned long sec; getSeconds( &sec ); /*print the actual value */ printf("Number of seconds: %ld\n", sec ); return 0; } void getSeconds(unsigned long *par) { /*get the current number of seconds*/ *par = time( NULL ); return; }
Output
Number of seconds: 1294450468
Function pointers
It is used for storing the address of a function. function pointers can also be called like a function in C.
Example
#include <stdio.h> int main(){ int num=123; int *pr2; //A normal pointer pr2 int **pr1;//This pointer pr2 is a double pointerpr2 = # /* Assigning the address of variable num to the * pointer pr2*/pr1 = &pr2; /* Assigning the address of pointer pr2 to the * pointer-to-pointer pr1*/ /* Possible ways to find value of variable num*/ printf("\n Value of num is: %d", num); printf("\n Value of num using pr2 is: %d", *pr2); printf("\n Value of num using pr1 is: %d", **pr1); /*Possible ways to find address of num*/ printf("\n Address of num is: %p", &num); printf("\n Address of num using pr2 is: %p", pr2); printf("\n Address of num using pr1 is: %p", *pr1);/*Find value of pointer*/ printf("\n Value of Pointer pr2 is: %p", pr2); printf("\n Value of Pointer pr2 using pr1 is: %p", *pr1);/*Ways to find address of pointer*/ printf("\n Address of Pointer pr2 is:%p",&pr2); printf("\n Address of Pointer pr2 using pr1 is:%p",pr1);/*Double pointer value and address*/ printf("\n Value of Pointer pr1 is:%p",pr1); printf("\n Address of Pointer pr1 is:%p",&pr1); return 0;}
Output
Value of num is: 123
Value of num using pr2 is: 123
Value of num using pr1 is: 123
Address of num is: XX771230
Address of num using pr2 is: XX771230
Address of num using pr1 is: XX771230
Value of Pointer pr2 is: XX771230
Value of Pointer pr2 using pr1 is: XX771230
Address of Pointer pr2 is: 66X123X1
Address of Pointer pr2 using pr1 is: 66X123X1
Value of Pointer pr1 is: 66X123X1
Address of Pointer pr1 is: XX661111
NULL Pointers
A pointer that is assigned NULL is called a null pointer. And the NULL pointer is a constant with a value of zero defined in several standard libraries.
Example
#include<stdio.h> int main() { int *ptr = NULL; printf("The value of ptr is : %x\n", ptr ); return 0; }
Output
The value of ptr is 0