I see this question a lot on SO. Maybe not in so many words… but time and again there is confusion on how arrays are different from pointers. So I thought I would take a moment to Q&A a few points about this.
For purposes of this Q&A we’re going to assume a 32-bit system and the following have been declared:
char * ptr = "hello";
char arr[10] = "hello";
int iarr[10] = {0};
Here’s a list of questions that surmise the confusion I see on SO. As I see new ones I’ll add to my list of Q&A (others feel free to as well, and correct me if you see any mistakes!)
- Isn’t a pointer and an array basically the same thing?
- Follow up: both
*(ptr)and*(arr), orptr[0]andarr[0]give the same thing, why? - How come
arrand&arris the same value? - Follow up: why do I get a different value printing
arr+1vs&arr+1?
1) Pointers are not arrays. Arrays are not pointers. Don’t think of them that way because they are different.
How can I prove this? Think about what they look like in memory:
Our array

arris 10 characters long. It contains “Hello”, but wait, that’s not all! Because we have a statically declared array longer than our message, we get a bunch of NULL characters ('\0') thrown in for free! Also, note how the namearris conceptually attached to the contiguous characters, (it’s not pointing to anything).Next consider how our pointer would look in memory:

Note here we’re pointing to a character array some place in read only memory.
So while both
arrandptrwere initialized the same way, the contents/location of each is actually different.This is the key point:
ptr is a variable, we can point it to anything, arr is a constant, it will always refer to this block of 10 characters.
2) The
[]is an “add and deference” operator that can be used on an address. Meaning thatarr[0]is the same as saying*(arr+0). So yes doing this:Would give you an output of “e e”. It’s not because arrays are pointers, it’s because the name of an array
arrand a pointerptrboth happen to give you an address.Key point to #2: The deference operator
*and the add and deference operator[]are not specific to pointers and arrays respectively. These operators simply work on addresses.3) I don’t have an extremely simple answer… so let’s forget our character arrays for a second and take a look at it this example for an explanation:
So that’s pretty understandable how about this:
What does that line of code tell you? If I deference
c, then I get an int. That means just plaincis an address. Since it’s the start of the array it’s the address of the array and also the address of the first element in the array, thus from a value standpoint:4) Let me go off topic for a second here… this last question is part of the confusion of address arithmetic. I saw a question on SO at one point implying that addresses are just integer values… You need to understand that in C addresses have knowledge of type. That is to say:
Basically the
sizeof(int)is 4 and thesizeof(char)is 1. So “adding 1 to an array” is not as simple as it looks.So now, back to the question, why is
arr+1different from&arr+1? The first one is adding1 * sizeof(char)=1 to the address, the second one is adding1 * sizeof(arr)=10 to the address.This is why even though they are both “only adding 1” they give different results.