Monday, 1 April 2013

sizeof pointer


    void main()
{
printf(“sizeof (void *) = %d \n“, sizeof( void *));
    printf(“sizeof (int *)    = %d \n”, sizeof(int *));
    printf(“sizeof (double *)  = %d \n”, sizeof(double *));
    printf(“sizeof(struct unknown *) = %d \n”, sizeof(struct unknown *));
    }
Answer   :
sizeof (void *) = 2
sizeof (int *)    = 2
sizeof (double *)  =  2
sizeof(struct unknown *) =  2
Explanation:
The pointer to any type is of same size.


Blog Author: Vijay Kumar

Is the following code legal? (5)


    Is the following code legal?

void main()
{
typedef struct a aType;
aType someVariable;
struct a
{
int x;
      aType *b;
              };
}
Answer:
         No
Explanation:
         When the declaration,
typedef struct a aType;
is encountered body of struct a is not known. This is known as ‘incomplete types’.

Blog Author: Vijay Kumar

Is the following code legal? (4)


    Is the following code legal?

typedef struct a aType;
struct a
{
int x;
aType *b;
};
Answer:
    Yes
Explanation:
The typename aType is known at the point of declaring the structure, because it is already typedefined.

Blog Author: Vijay Kumar

Is the following code legal? (3)


    Is the following code legal?
typedef struct a
    {
int x;
 aType *b;
    }aType
Answer:
         No
Explanation:
The typename aType is not known at the point of declaring the structure (forward references are not made for typedefs).

Blog Author: Vijay Kumar

Is the following code legal? (2)


 Is the following code legal?
struct a
    {
int x;
            struct a *b;
    }
Answer:
Yes.
Explanation:
*b is a pointer to type struct a and so is legal. The compiler knows, the size of the pointer to a structure even before the size of the structure
is determined(as you know the pointer to any type is of same size). This type of structures is known as ‘self-referencing’ structure.

Blog Author: Vijay Kumar