Pointer

A pointer is a variable that store the address of another variable direct address of the memory location. Thus, the variable can be type of int, char , array, function or any other pointer. Variable or constant you must declare a pointer before using it to store any variable address.However, in 32-bit architecture the size of a pointer is 2 byte.

The following example to define a pointer which stores the address of an integer

int n=20;
int*p = &n;

Declaring a pointer

In C language pointer can be declared using * (asterisk symbol).

int*a;
char *c;

Let’s see the example of pointer

#include <stdio.h> 
int main() 
{ 
     
    int Var = 10;  
    int *ptr = &Var;
    printf("Value of Var = %d\n", *ptr); 
    printf("Address of Var = %p\n", ptr); 
    *ptr = 20;  
    printf("After doing *ptr = 20, *ptr is %d\n", *ptr); 
  
    return 0; 
}

Output

Value of Var = 10
Address of Var = 0x7fffa057dd4
After doing *ptr = 20, *ptr is 20

Pointer to array

int arr[10];
int *p[10]=&arr;

Pointer to function

void show(int);
void(*p)(int)=&display;

Pointer to structure

struct st{
int i;
float f;
}ref;
struct st*p = &ref;

Address Of “&” operator

#include<stdio.h>  
int main(){  
int number=50;   
printf("value of number is %d, address of number is %u",number,&number);    
return 0;  
}

Output

value of numberis 50, address of number is fff4.

Reading complex pointers

let’s see the precedence and associativity of the operators which are used regarding pointers.

Operator Precedence AssociativityScopeLifetime
(),[] 1left to rightLocalWithin function
identifier , =2right to leftGlobalTill the end of the main program maybe declared anywhere in the program.
Data type 3-LocalTill the end of the main program, retains value between multiple functions call.

Submit your review
1
2
3
4
5
Submit
     
Cancel

Create your own review

The Technical Funda
Average rating:  
 0 reviews