1
char buffer[]="foobar"; 

I know that buffer is char* pointer to the first element so buffer==&buffer[0] but why &buffer==buffer? &buffer should give the memory address of the buffer char* and NOT the address of the first element?

Additionaly,What would happen when i do (int)buffer ?

7
  • 3
    The memory address of the buffer is the Memory address of the first element. However &buffer and buffer do in fact have different datatypes, so you should at least get a warning if you actually code up the Expression &buffer == buffer. Try it. Commented Apr 10, 2014 at 8:53
  • 1
    If you try (int)buffer, the compiler will first turn buffer into a pointer and then intepret the bits of that pointer as and int. This might also result in a warning if the pointer is bigger than the int, but I am not sure. Commented Apr 10, 2014 at 8:55
  • 1
    buffer is not a pointer (char *) - it's an array (char []) - try doing buffer++ if you want to see one important difference. It will decay to a pointer is various cases though. Commented Apr 10, 2014 at 8:57
  • 2
    Please don't make radical changes to your question text ... it confuses people who first view this after the changes are made. Commented Apr 10, 2014 at 10:54
  • 1
    Please avoid deleting your question after it was answered. SO is meant to be an archive of questions with answers. Just the answers would be rather pointless. Commented Apr 10, 2014 at 11:21

2 Answers 2

1

buffer is the address of the first element and &buffer is indeed the address of the array itself. The array will be stored on the stack directly. That is why &buffer == buffer.

It is not a pointer but an array. If you had declared it as char*, it would not be &buffer == buffer

Sign up to request clarification or add additional context in comments.

Comments

0

Think of it like this.

buffer is the address of the first element of the array. So it is the address of an integer.

&buffer is the address of the array. Hence &buffer will be in fact the same as buffer, but their behaviour will be different.

For e.g. if you do buffer+1, it will increment by the size of int, but &buffer+1 will increment by the size of the array, i.e size of one element * number of elements.


Edits. Initially I had written buffer++ instead of buffer+1. See comments section for the reason I edited it.

4 Comments

@undur_gongor I don't think Paul said anything about not doing buffer++. He said that it is an array, that decays to pointer. Hence, IMO, buffer++ is totally valid.
@undur_gongor My Bad. Thanks for bringing it to the notice. Just edited my answer.
Removed downvote (I suggest deletion of the history stuff and all the related comments).
Could you please improve your answer a little bit further? In C, a pointer is not just an address, it also carries the type information. The two addresses are the same, the types not. So the pointers are different.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.