What is pointer in c programming?


Explain pointers in c

Pointer is a user defined data type which creates special types of variables which can hold the address of primitive data type like char, int, float, double or user defined data type like function, pointer etc. or derived data type like array, structure, union, enum.
Examples:


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


In c programming every variable keeps two type of value.
1. Contain of variable or value of variable.
2. Address of variable where it has stored in the memory.


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

Explanation:

About variable a:
1. Name of variable : a
2. Value of variable which it keeps: 5
3. Address where it has stored in memory : 1025 (assume)

About variable ptr:
4. Name of variable : ptr
5. Value of variable which it keeps: 1025
6. Address where it has stored in memory : 5000 (assume)

Pictorial representation:




Note: A variable where it will be stored in memory is decided by operating system. We cannot guess at which location a particular variable will be stored in memory.

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


Explanation:

About variable a:
1. Name of variable : a
2. Value of variable which it keeps: 50
3. Address where it has stored in memory : 5000 (assume)

About variable ptr1:
4. Name of variable : ptr1
5. Value of variable which it keeps: 5000
6. Address where it has stored in memory : 9000 (assume)

About variable ptr2:
7. Name of variable : ptr2
8. Value of variable which it keeps: 9000
9. Address where it has stored in memory : 9555 (assume)

Pictorial representation of above pointer declaration and definition:



Note:
* is known as indirection operator which gives content of any variable.
& is known as reference operator which gives address where variable has stored in memory.


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

But it is not right to write:
&*p=p

Simple example:


What will be output of following c program?
#include<stdio.h>
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
Explanation:
As we know value of variable x is 25.


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


**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

12 comments:

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;
ptr=&x;

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...

@Heena:
**temp= **(&ptr)=*(*&ptr)=*ptr=*(&x)=*&x=x=25

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

**temp = **(&ptr)=25 (*and& will canceled)
**temp=*ptr=25
**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

sandeepsiv said...

very informative...thanxxx very much

Qasim Bilal said...

I have just started learning computer programming. I found this website very informative. It will help me to get some good programming concepts.
java tutorial

Nagarajan said...

hello, Sir very nice work , first of all lot of thanks
i have doubt in pointer .. pls help me


char c[]="GATE2011";
char *p=c;
printf("%s",p+p[3]-p[1]);


please explain this pointer concept .....

Eran Smith said...

C is the base of every programming language.and its a good post to clear the basic concepts.
.net Obfuscator

Dheeraj said...

This is really gud. Keep it up with more tutorials.
I am really a big fan of those personalities, who are always interested to share best part of their knowledge.

AVINASH SINGH said...

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

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