Interview Questions

But I heard that char a[] was identical to char *a.

C Interview Questions and Answers


(Continued from previous question...)

But I heard that char a[] was identical to char *a.

Not at all. (What you heard has to do with formal parameters to functions.) Arrays are not pointers, though they are closely related and can be used similarly .
The array declaration char a[6] requests that space for six characters be set aside, to be known by the name ``a''. That is, there is a location named ``a'' at which six characters can sit. The pointer declaration char *p, on the other hand, requests a place which holds a pointer, to be known by the name ``p''. This pointer can point almost anywhere: to any char, or to any contiguous array of chars, or nowhere
As usual, a picture is worth a thousand words. The declarations
char a[] = "hello";
char *p = "world";

would initialize data structures which could be represented like this:


   -------------------------
a: | h | e | l | l | o | 0 |
   -------------------------

   ------     -------------------------
p: | *--|---> | w | o | r | l | d | 0 |
   -----      -------------------------
   

It is useful to realize that a reference like x[3] generates different code depending on whether x is an array or a pointer. Given the declarations above, when the compiler sees the expression a[3], it emits code to start at the location ``a'', move three past it, and fetch the character there. When it sees the expression p[3], it emits code to start at the location ``p'', fetch the pointer value there, add three to the pointer, and finally fetch the character pointed to. In other words, a[3] is three places past (the start of) the object named a, while p[3] is three places past the object pointed to by p. In the example above, both a[3] and p[3] happen to be the character 'l', but the compiler gets there differently. (The essential difference is that the values of an array like a and a pointer like p are computed differently whenever they appear in expressions, whether or not they are being subscripted,

(Continued on next question...)

Other Interview Questions