Pointers :

What is pointer ?

A pointer is but a variable-like name which pointer or represents a storage location in memory (RAM). Remember that the RAM has many cells to store values. Each cell in memory is 1 byte and has a unique address to identify it.

Example :

  1. /* Description: find a biggest number use pointer */
  2. #include<stdio.h>
  3. main()
  4. {
  5. int a,b,*big,*xptr,*yptr;
  6. printf("\n Enter two integers : ");
  7. scanf("%d %d",&a,&b);
  8. xptr=&a;
  9. yptr=&b;
  10. if(*xptr > *yptr)
  11. big = xptr;
  12. else
  13. big = yptr;
  14. printf("\n Biggest number is %d",*big);
  15. getch();
  16. }

Output :

c language - find a biggest number use pointer

pointer declaration :

a pointer is declared like a variable with appropriate data type. The pointer variable in the declaration is preceded by the *(asterisk) symbol.

Example :

int *mptr, *n;
float *xp, *yp, *zp;
char *p, *c[20]


Address Operator & :

The symbol & ( ampersand ) is an address operator which is used to access the address of a variable and assign it to a pointer to initialize it.

Example :

int m = 15, *mptr;
float x = 2.54, *xptr;

mptr = &m;
xptr = &x;


Note : The pointer maybe initialized in the declaration statement as shown below :

int m = 15, *mptr = &m;


indirection Operator * :

The symbol * ( asterisk ) is an indirection operator which is used to access the value of a variable through a pointer.

int m = 15, *mptr;
float x = 2.54, *xptr;
mptr = &m;
xptr = &x;
printf("\n Value of m = %d", *mptr);
printf("\n Value of m = %0.2f", *xptr)


When this program is executed, the values referred by the pointers are printed as shown below :

value of m = 15;
value of x = 2.54;


Note : That an address of a variable should be assigned to a pointer before the indirection operator along with the pointer is used in any manipulation.


pointer expressions :

ExpressionMeaningResult
&iaddress of i65490
&jaddress of j65492
pjvalue stored in pj
( which is a pointer address )
65492
*pjvalue referred by pj65
*pivalue referred by pi65
*pi + 2value referred by pi + 2 = 65+267
*(pi - 1)value referred in (pi - 1)
(=65492 - 1*sizeof(int)=65490)
35
(pi + 2)new value in pointer arithmetic (pi + 2)
(=65492 + 2*sizeof(int)=65496)
65496
ivalue of i35
pivalue stored in pi
(which is also a pointer address)
65492

Next Update

Loading...

Currently we improved our website User Interface and other contents. Our next update is adding video tutorials on this site so, We create a new YouTube channel, our YouTube channel name is CodeX.

We need some time for make video tutorials (CodeX) so, stay with us and keep supporting and you know already we make a Collaboration with HackIsOn YouTube channel long time ago So, please Subscribe Our Channels