A pointer in c is used to store the address of any variable. It directly points to the memory location.
By using a pointer, we can work faster on memory than a variable.
int a=5;
(i) it reserves space in memory to hold integers.
(ii) The Memory location is a.
(iii) The value at the memory location is 5.
How to declare a pointer in c?
int *p1 /*Pointer to an integer variable*/
double *p2 /*Pointer to a variable of data type double*/
char *p3 /*Pointer to a character variable*/
float *p4 /*Pointer to a float variable*/
Explanation of C Pointer with examples
Example 1: Print the address of a given variable in C Pointer
#include <stdio.h>
int main()
{
int a=5;
printf("Address of a=%p\n",&a);
printf("The value of a is=%d\n",a);
return 0;
}
Output
Address of a=0x7ffed0516f24
The value of a is=5
Example 2: Print the address of the Given variable in C
#include <stdio.h>
int main()
{
int *a;
int var = 20;
a= &var;
printf("Value of variable var is: %d", var);
printf("\nValue of variable var is: %d", *a);
printf("\nAddress of variable var is: %p", &var);
printf("\nAddress of variable var is: %p", a);
printf("\nAddress of pointer p is: %p", &a);
return 0;
}
Output
Value of variable var is: 20
Value of variable var is: 20
Address of variable var is: 0x7fff7ed94c3c
Address of variable var is: 0x7fff7ed94c3c
Address of pointer p is: 0x7fff7ed94c40
Some Important notes on Pointer in C
1) Pointer to Pointer- A reminder can point to one more tip (which indicates it can save the address of an additional guideline), such reminders are called double tip OR pointer to a pointer.
2) Passing a pointer to function- Reminders can also be passed as an argument to a feature, utilizing this function a feature can be called by referral as well as a variety that can be passed to a function while calling.
3) Function pointers- A featured reminder is just like an additional reminder, it is utilized for storing the address of a function. Function tips can also be utilized for calling a feature in the C program.