pointer

Pointer in C/C++

Avinash Pandey

A pointer is a constant or variable that contains an address of another variable of same type and in future, same address can be used to access the variable.

Syntax for pointer declaration:



So, using above syntax we can declare any type (built-in or derived) of pointer variable. In following figure, three built-in type of pointer variable has been created and they can store address of another variable of same type.

In the above figure, p is a character pointer and it can only store address of any character variable [as per pointer definition].
Let us take an example:

char a;
char *p;
int n;

then, p can store address of a, but it cannot store address of n, because n is integer variable. To store address of integer variable, integer pointer variable is required i.e.

int *p;


Physical and logical representation of statement p=&a has been given in following figure. From the physical representation it can be easily observed that pointer p is pointing to variable a. In other words, we can say p has contained the address of a (p=&a).
pointer
Let us assume we have three pointer of variable as given in following figure. What will be size of each pointer variable for 32 bit compiler?
pointer
sizeof(p)=?
sizeof(q)=?
sizeof(r)=?
As we all know that pointer stores address of other variable of same type and address is represented using integer type. So, the size of integer pointer, float pointer, char pointer or any other type of pointer will be equal to size of integer variable. For 32 bit compiler size of integer is equal to 4. Therefore, size of any type of pointer variable will be 4.
sizeof(p) = sizeof(q) = sizeof(r) = sizeof(int) => 4 byte.

void main()
{
char *p;
int *q;
float *r;
void *s;
printf("sizeof(p)=%d\nsizeof(q)=%d\nsizeof(r)=%d\nsizeof(s)=%d",
       sizeof(p),sizeof(q),sizeof(r),sizeof(s));
}

pointer

To understand array from memory aspect click here.

What do you think about the article?

This site uses Akismet to reduce spam. Learn how your comment data is processed.