Pointers store address of variables or a memory location.
datatype *var_name;
int *ptr;
// An example pointer “ptr” that holds address of an integer variable or holds address of a memory whose value(s) can be accessed as integer values through “ptr”.
#include <stdio.h>
void main()
{
int x = 10;
int *ptr;
ptr = &x;
printf(“Value is : %d”,*ptr);
}
- Since there is * in declaration, ptr becomes a pointer varaible (a variable that stores address of another variable)
- Since there is int before *, ptr is pointer to an integer type variable int *ptr;& operator before x is used to get address of x. The address of x is assigned to ptr.
void main()
{
int *p;
int var = 10;
p= &var;
printf("Value of variable var is: %d", var);
printf("\nValue of variable var is: %d", *p);
printf("\nAddress of variable var is: %d", &var);
printf("\nAddress of variable var is: %d", p);
printf("\nAddress of pointer p is: %d", &p);
}
Output:
Value of variable var is: 10
Value of variable var is: 10
Address of variable var is: 0x7fff5ed98c4c
Address of variable var is: 0x7fff5ed98c4c
Address of pointer p is: 0x7fff5ed98c50
/* Pointer of integer type, this can hold the * address of a integer type variable. */
/* Assigning the address of variable var to the pointer * p. The p can hold the address of var because var is * an integer type variable. */