• TCS: Predict the output



    main()
    {
                char *p;
                printf("%d %d ",sizeof(*p),sizeof(p));
    }

    Answer:
                            1 2

    Explanation:
                        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.

1 comments:

  1. Anonymous said...

    output of sizeof(*p) is 1 and output for sizeof(p) would be implementation defined.
    http://stackoverflow.com/questions/399003/is-the-sizeofsome-pointer-always-equal-to-four

Post a Comment