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


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

Explanation:

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;
ptr1=&a;
ptr2=&pt1;


Explanation:

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

13 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

AVINASH SINGH said...

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

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

tarun trivedi said...

niceeeeeee.......

mohit said...

This comment has been removed by the author.

mohit said...

when i run the program in c
int *ptr;
ptr=new int;
*ptr=5;
cout<<(*&ptr)<<endl;
cout<<ptr<<endl;
cout<<(*ptr)<<endl;
cout<<(&*ptr)<<endl;
delete ptr;
It gives the output as
0x8d20008
0x8d20008
5
0x8d20008
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);