Examveda
Examveda

What will be printed after compiling and running the following code?
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.


This Question Belongs to C Program >> Pointer

Join The Discussion

Comments ( 3 )

  1. Pratik Kumar
    Pratik Kumar :
    7 years ago

    main()
    {
    char *p;
    printf("%d %d",sizeof(*p), sizeof(p));
    }
    ANSWER IS : 1 , 8

  2. Examveda
    Examveda :
    8 years ago

    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

  3. Sri Nidhi
    Sri Nidhi :
    8 years ago

    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.

Related Questions on Pointer