C/C pointers
Expert: Zlatko - 5/29/2011
QuestionHi this is Bhavan
can you explain this pointer always stores the starting address of a variable why it occupies different sizes to store address of integer,float and any other user defined type?
AnswerHello Bhavan
A pointer always has one size, and the size of the pointer does not depend on what it is pointing to. Generally a pointer is 4 bytes long, but it might be 8 bytes long on a 64 bit system. However, on any particular system, all pointers are the same size.
You can check the size of your datatypes using the sizeof operator. For example, to get the size of different types, use this
#include<stdio.h>
int main()
{
printf("char size %d\n", sizeof(char));
printf("char pointer size %d\n", sizeof(char*));
printf("int size %d\n", sizeof(int));
printf("int pointer size %d\n", sizeof(int*));
printf("double size %d\n", sizeof(double));
printf("double pointer size %d\n", sizeof(double*));
return 0;
}
You should see output like this:
char size 1
char pointer size 4
int size 4
int pointer size 4
double size 8
double pointer size 4
I hope that helps you.
Best regards
Zlatko