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.
|
a pointer is declared like a variable with appropriate data type. The pointer variable in the declaration is preceded by the *(asterisk) symbol.
int *mptr, *n;
float *xp, *yp, *zp;
char *p, *c[20]
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.
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;
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.
Expression | Meaning | Result |
---|---|---|
&i | address of i | 65490 |
&j | address of j | 65492 |
pj | value stored in pj ( which is a pointer address ) | 65492 |
*pj | value referred by pj | 65 |
*pi | value referred by pi | 65 |
*pi + 2 | value referred by pi + 2 = 65+2 | 67 |
*(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 |
i | value of i | 35 |
pi | value stored in pi (which is also a pointer address) | 65492 |
It's a special area where you can find special questions and answers for CSE students or IT professionals. Also, In this section, we try to explain a topic in a very deep way.