14.5 – Type of pointer and void pointer

by subbu on January 18, 2014

In previous sessions we have learned that, the type of pointer must be equivalent to the type of variable. Now we will see why is it so?
We know that, any pointer is stored with the upper byte number of allocated memory. While accessing the data through dereference (through pointer using value at * operator), the type of pointer decides up to how many bytes the data must read. say for example if the pointer is of short type then the data will read from adjacent two bytes, if the pointer is of float type then the data will read from adjacent four bytes and if the pointer is of char type then the data will read from first one byte

short x=10453,*p=&x;
printf("%d",*p);

short pointer

Here the pointer p reads the data from adjacent two bytes from the first byte because short type takes two bytes.

float x=12.345676,*p=&x;
printf("%f",*p);

float pointer

Here pointer p reads the data from adjacent four bytes from the first byte because float type takes four bytes.

char ch='a',*p=&ch;
printf("%c",*p);

Char pointer

Here pointer p reads the data from only one byte because the char type takes only one byte.

Now we will see what happens when we store the address of one type of variable into another type of pointer.

#include<stdio.h>
int main()
{
short x=13457;
char *p=&x;
printf("\n%d",*p);
printf("\n%d",*(p+1));
return 0;
}

Output:
-111
52

pointer mismatch

Here the character pointer points the short type of memory allocation. It can read the data from only one byte because the char type takes only one byte.

*p reads the data only from lower byte 1001 0001, equivalent decimal value is -111. *(p+1) will read the value of upper byte 0011 0100, equivalent decimal value is 52

void pointer:

It is a type introduced by ANSI. It is generally used as a generic pointer to store the address of any type of variable but, can’t access the value of variable using dereferencing because the void pointer doesn’t know what type of objects it is pointing to. We need to typecast void pointer type to variable pointer type before performing indirection or dereferencing.

short x=25;
float y=345.879656;
char ch='a';
void *p=&x;        /* pointing short type object */
void *q=&y;        /* pointing float type object */
void *r=&ch;       /* pointing char type object  */

void pointer

#include<stdio.h>
int main()
{
short x=25;
float y=345.879656;
char ch='a';
void *p=&x,*q=&y,*r=&ch;
printf("\nProgram successfully executed..");
return 0;
}

Execution:
Program successfully executed..

Here nothing went wrong while assigning the addresses of different variable of different types to void pointers

#include<stdio.h>
int main()
{
short x=25;
float y=345.879656;
char ch='a';
void *p=&x,*q=&y,*r=&ch;
printf("x=%d",*p);
printf("\ny=%f",*q);
printf("\nz=%c",*r);
return 0;
}

Execution:
Error pointer1.c 8: Not an allowed type in function main
Error pointer1.c 9: Not an allowed type in function main
Error pointer1.c 10: Not an allowed type in function main

But, here is problem while accessing the values of variables through void pointers using dereference

To rectify the problem, we need to typecast void pointer to variable type pointers to access the values of variables using dereference

#include<stdio.h>
int main()
{
short x=25;
float y=345.879656;
char ch='a';
void *p=&x,*q=&y,*r=&ch;
printf("x=%d",*((short*)p));         /* type casting to short*   */
printf("\ny=%f",*((float*)q));    /* type casting to float* */
printf("\nz=%c",*((char*)r));     /* type casting to char* */
return 0;
}

Execution:
x=25
y=345.879669
z=a

Size of void pointer:

Though the size of void type is 0, the size of void pointer is the same size as any other pointer type

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

Output in Turbo C 3.0:
Size of void pointer 2

Output in Ubuntu Linux gcc:
Size of void pointer 4

Previous post:

Next post: