Thursday, April 18, 2013

C/C++ interview question --- question 3

Why we define pointer with a type in C?

A pointer only contains an address then why do we need different data types for it ? why not a single data type for all pointers?

ANS:

Pointer data type is used in pointer arithmetic.

e.g. :

int * p1;
char * p2;

p1 = p1 + 1;
p2 = p2 +1;

value of p1 increases by sizeof(int) which is 2 bytes and p2 increases by sizeof(char) which is 1 bytes.

No comments:

Post a Comment