What is pointer in c programming?

Explain pointers in c

A pointer is a user-defined data type that allows the creation of specialized variables capable of storing the memory address of primitive data types such as char, int, float, double, or user-defined data types like functions and pointers, as well as derived data types like arrays, structures, unions, and enums.

int *ptr;

int (*ptr)();
int (*ptr)[2];

In C programming, every variable holds two types of values:

  1. 1. Content of the Variable: This refers to the actual value stored in the variable.
  2. 2. Address of the Variable: This is the memory address where the variable is stored.

(1) Meaning of following simple pointer declaration and definition:
int a=5;
int * ptr;


Regarding variable 'a':

  1. 1. Name of Variable: a
  2. 2. Value Contained: 5
  3. 3. Memory Address: 1025 (assumed)

Regarding variable 'ptr': 4. Name of Variable: ptr

  1. 1. Value Contained: 1025
  2. 2. Memory Address: 5000 (assumed)

Pictorial representation:

Note: The location in memory where a variable is stored is determined by the operating system. It is not possible to predict the exact location where a particular variable will be stored in memory.

(2) Meaning of following pointer declaration and definition:
int a=50;
int *ptr1;
int **ptr2;


Regarding variable 'a':

Name of Variable: a
Value Contained: 50
Memory Address: 5000 (assumed)
Regarding variable 'ptr1':
4. Name of Variable: ptr1

Value Contained: 5000
Memory Address: 9000 (assumed)
Regarding variable 'ptr2':
7. Name of Variable: ptr2

Value Contained: 9000
Memory Address: 9555 (assumed)

Pictorial representation of above pointer declaration and definition:

  • * is recognized as the indirection operator, providing the content of any variable.
  • & is identified as the reference operator, furnishing the address where the variable is stored in memory.

Cancellation rule of above two operators:
* and & operators always cancel to each other i.e.

But it is not right to write:

Simple example:

What will be output of following c program?
int main(){

    int x=25;
    int *ptr=&x; //statement one
    int **temp=&ptr; //statement two
    printf(“%d %d %d”.x.*ptr,**temp);
    return 0;

Output: 25 25 25
As we know value of variable x is 25.

*ptr= *(&x) //from statement one
=x //using cancellation rule

**temp= **(&ptr)=*(*&ptr)=*ptr=*(&x)=*&x=x=25

Definition of pointer
How to read complex pointer
Arithmetic operation with pointer
Pointer to function
Pointer to array of function
Pointer to array of string
Pointer to structure
pointer to union
Multi level pointer
Pointer to array of pointer to string
Pointer to three dimentional array
Pointer to two dimensional array
Sorting of array using pointer
Pointer to array of array
Pointer to array of union
Pointer to array of structure
Pointer to array of character
Pointer to array of integer
Complex pointer
Generic pointer
Null pointer
Wild pointer
Dangling pointer
Near pointer
Far pointer
Graphics video memory
Text video memory
Huge pointer
Memory model in C
C tutorial


Suresh said...

Good questions to learn pointers....

nidhi said...

can u xplain me y u write
int *ptr=&x;
as dis
*ptr= *(&x)

krp said...

you can write it another way
int x;
int *ptr;

in short you can write

int x;
int *ptr=&x;

that's all

heena said...

**temp= **(&ptr)=*(*&ptr)=*ptr=*(&x)=*&x=x=25
whats the meaning of this line

Afiz S said...

**temp= **(&ptr)=*(*&ptr)=*ptr=*(&x)=*&x=x=25

let me divide this into small pieces. left to right
*&x=x // x=25
x=25 (*and& will cancellation rule)
jut like that finally you will get

**temp = **(&ptr)=25 (*and& will canceled)
**temp=25; (temp is pointer to another pointer variable called ptr)

*temp: address of pointer ptr
**temp : is content of x variable

Satheesh said...

nice work.............thanks


c programming language is simple and the best language............

u can solve any problem here...
but u should knowledge about "c"

tarun trivedi said...


mohit said...

This comment has been removed by the author.

mohit said...

when i run the program in c
int *ptr;
ptr=new int;
delete ptr;
It gives the output as
but as in this tutorial it is written that it is not right to write &*p=p;
but the above result shows that its right???

Sandwich Enthusiast said...

Mohit you are mixing dialects of C...the snippet you ionserted is C++, and pointer notation in C++ isn't the same as C

sumaya zama Syed said...

Thank u soooooooooooooo much for helping me understand the concept of pointers & i hav a small doubt that is,is this a formula for pointer"a=5;ptr=&a"right......?plz send me rply

bimal shah said...

Thanks a lot for such an excellent clear concept. Really,sir....u r the master...sir i have small doubt in line[printf(“%d %d %d”.x.*ptr,**temp); ] is it .x. Or ,x, i.e [ printf(“%d %d %d”,x,*ptr,**temp);