14.2 – What is pointer?

by subbu on January 15, 2014

What is pointer?

Pointer is a variable which holds the address of another variable.

short x=345;

It declares a short type of variable x and which is being assigned with an integer constant 345.

p=&x;

In the above expression &x gives the address of the variable x, which is being assigned to another variable p. Here p is not an ordinary variable, which holds the address of another variable x. So, it is called a pointer variable.

Pointer

Even we need to declare the pointer variable p to provide some space in the memory.

short *p;

It is the pointer declaration statement. It tells the compiler that p will be used to store the address of a short type of variable.

How to declare a pointer:

Declaration of pointer differs from the declaration of a normal variable. While declaring the pointer makes sure that,

  • The name of pointer must be prefixed with the symbol * so that, the compiler restricts the variable only to store the address of another variable.
  • The type of pointer must be same as the type of variable whose address will be stored.

pointer diclaration

Say for example, a short pointer is needed to hold the address of a short variable.

short *p;  /* short pointer to store the address of a short variable */

A float pointer is needed to hold the address of a float variable.

float *q; /* float pointer to store the address of a float variable */

Assigning the address to the pointer

short *p;

Declaration of variable

It declares a short pointer to store the address of another short type of variable

short x=345;

Assigning the address

It declares a short variable x and which is assigned with 345.

p=&x;

clip_image005

Here the address of x is being assigned to the pointer. The value of p and the address of x are same (65522)

#include <stdio.h>
int main()
{
short *p;       /* declaration of short pointer */
short x=345;  /* declaration of a short variable */
p=&x;        /* assigning the address to the pointer */
printf("\nAddress of x %u",&x);  /* address of x */
printf("\nAddress of x %u\n",p); /* address of x in pointer p */
return 0;
}

Execution:
Address of x 65522
Address of x 65522

What would be the output following program?

#include<stdio.h>
int main()
{
short x=10,y=20;
short *p=&x;
short* q=&y;             /* asterisk symbol is used after data type */
printf("\nx=%d",*p);
printf("\ny=%d\n",*q);
return 0;
}
Show Answer
Output:
x=10
y=20

The meaning of pointer declaration would not be changes though we write asterisk beside data type

Size of a pointer

As we know, address of a memory allocation is a 16bit number in case of a 16bit system and 64bit in case of a 64bit system. Hence the size of pointer must be the size of address because the purpose of pointer is to store the address.

The size of any pointer of any type is the same that is, the sizes of char*, int*, float* and double* are same.

#include <stdio.h>
int main()
{
short *p;
float *q;
double *r;
printf("Size of short pointer %d bytes",sizeof(p));
printf("\nSize of float pointer %d bytes",sizeof(q));
printf("\nSize of double pointer %d bytes",sizeof(r));
return 0;
}

Output in Turbo C:
Size of short pointer 2 bytes
Size of float pointer 2 bytes
Size of double pointer 2 bytes

Output in Linux gcc:
Size of short pointer 4 bytes
Size of float pointer 4 bytes
Size of double pointer 4 bytes

#include <stdio.h>
int main()
{
printf("Size of short pointer %d bytes",sizeof(short*));
printf("\nSize of float pointer %d bytes",sizeof(float*));
printf("\nSize of double pointer %d bytes",sizeof(double*));
return 0;
}

Output in Turbo C:
Size of short pointer 2 bytes
Size of float pointer 2 bytes
Size of double pointer 2 bytes

Output in Linux gcc:
Size of short pointer 4 bytes
Size of float pointer 4 bytes
Size of double pointer 4 bytes

Pointer to register variable

As we have discoursed in the previous session, we can’t access the address of a register variable using address of operator &. Hence we can’t create a pointer to the register variable.

#include<stdio.h>
int main()
{
register short x=345;
short *p=&x;
printf("\nValue of x %d",x);
printf("\nAddress of x %u",&x);
return 0;
}

Output:
Error pointer1.c 5: Must take address of a memory location in function main
Error pointer1.c 7: Must take address of a memory location in function main
*** 2 errors in Compile ***

Previous post:

Next post: