What will be printed after compiling and running the following code?
main()
{
char *p;
printf("%d %d",sizeof(*p), sizeof(p));
}
main()
{
char *p;
printf("%d %d",sizeof(*p), sizeof(p));
}
A. 1 1
B. 1 2
C. 2 1
D. 2 2
Answer: Option B
Solution(By Examveda Team)
The sizeof() operator gives the number of bytes taken by its operand. p is a character pointer, which needs one byte for storing its value (a character). Hence sizeof(*p) gives a value of 1. Since it needs two bytes to store the address of the character pointer sizeof(p) gives 2.
Join The Discussion
Comments ( 3 )
Related Questions on Pointer
In C, what is a pointer primarily used for?
A. Decision making
B. Code organization
C. Variable declaration
D. Storing values
In C, how do you declare a pointer variable that can store the address of an integer?
A. int *ptr;
B. ptr int;
C. int ptr;
D. ptr *int;
What is the purpose of the '->' operator in C when used with pointers?
A. Arithmetic operation
B. Indirection operator
C. Member access operator
D. Bitwise operation
main()
{
char *p;
printf("%d %d",sizeof(*p), sizeof(p));
}
ANSWER IS : 1 , 8
The size of the pointer is equal to the size of integer because pointer always holds address values and address are integer.The size of integer depends upon many things like architecture, operating system etc because of which you are getting different value.
Size of pointer in different IDE
Compiler ---------- Size of Integer ------ Size of Integer Pointer
Borland C/C++ ----- 2 Bytes ------ 2 Bytes
Turbo C/C++ ----- 2 Bytes ------ 2 Bytes
Visual C++ ----- 4 Bytes ------ 4 bytes
size of any pointer is 4bytes for 32bit machine and 8 bytes for 64 bit machine...in ur solutions mentioned it has 2 bytes in most of sizeof pointer calculations.