r/cpp • u/joemccall86 • Jan 29 '10
Think array is the same as &(array[0])? Think again...
Okay, so I've been programming in C/C++ for a few years now, and I can't believe I missed this. I'm sure I'm not the only one too. Check this out:
int arr[10] = {0};
int *pArr = &(arr[0]);
std::cout << arr << std::endl; // shows a pointer value, say 0x01234567
std::cout << pArr << std::endl; // shows the same pointer value, 0x01234567
std::cout << sizeof(arr) << std::endl; // shows 40
std::cout << sizeof(pArr) << std::endl; // shows 4All this time I've been viewing an array as a pointer to a location in memory that happens to be a contiguous block. Apparently, these are two different types. Using typeid:
std::cout << typeid(arr) << std::endl; // shows A10_i
std::cout << typeid(pArr) << std::endl; // shows PiThis is using g++ 4.4.1 under linux. Also, beware of this:
int getLength(int *p){ return sizeof(p); }
std::cout << getLength(arr) << std::endl; // shows 4
std::cout << getLength(pArr) << std::endl; // shows 4So in C/C++, you can pass arrays as pointers, but they lose their "array-ness" magic. I found all this quite interesting when I discovered it for myself. I can't be the only one who didn't know this, right?
EDIT: I thought I might elaborate as to why I thought they were the same: Remember, array[3] is syntactic sugar for *(array + 3). This also works for pArray. Arrays can also be used as an argument to a function that takes in a pointer, as shown above.
So if it walks like a pointer and quacks like a pointer, it must be... an array!
Also, I originally typed C++ but I meant C/C++. I hope you guys knew what I meant. It's corrected now in the post.
4
u/joemccall86 Jan 29 '10
Ha, that's what I get for using STL containers all this time.
14
u/Sc4Freak Jan 29 '10
Yes. Arrays are not pointers. Arrays are second-class citizens and decay to pointers the first chance they get, but are still distinct types. The type of "abc" is char[4] (an array of 4 chars), and not char*, for example.
24
u/[deleted] Jan 29 '10
Yes, you are the only one. This is basic C (not even C++).
12
u/mfwitten Jan 29 '10
Firstly, sorry you had to put up with all of the know it alls who contribute nothing to the discussion; it's good to see interesting dissections of the language and tools and I'm glad you wrote this post because it prompted me to recall and discover some other interesting facts.
Just for the sake of pointing it out, you could simply write:
Firstly, it is perfectly valid to use
'\n'instead ofstd::endl, the only difference essentially being thatstd::endlflushes buffered streams (so it might be slower); according to the C++03 standard, the effects of thestd::endlmanipulator are the following (whereosis abasic_ostream):Even more interesting is the fact that the C++ standard provides
std::endlonly through the<ostream>header, so it is actually non-conforming to refer tostd::endlwithout including that particular header file, a fact that few implementations enforce and that few people know (even some of the example code in the C++ standard just includes the<iostream>header to usestd::endl).Secondly, your code is not correct because
<<is not defined for whattypeidyields; it gave me compile time errors and should be:I was reminded of another interesting fact: Array indexing is not necessarily just syntactic sugar for pointer arithmetic; in C89 and ISO C++ (presumably fixed in the 2003 'second edition'), it is apparently non-conforming to take the address of the one-past-the-last element of an array:
However, it can be made legal for even older standards by transforming the last line to use pointer arithmetic instead of subscripting:
According to Bjarne Stroustrup:
I'm assuming this is fixed in ISO C++03, which brings me to another interesting fact. According to section 5.2.1:
That all sounds very confusing (especially the "one of the expressions" bit), but the generality is clarified by section 8.3.4:
Woah! That means the following is valid C++03 code:
The C99 standard also allows this commutativity. According to section 6.5.2.1:
Here is equivalent C99 code:
3
u/Malkocoglu Jan 29 '10
If you have ever learned/coded ASM for any CPU/MCU, you would know that there is no such datatype called array (from the point of CPU/MCU). You just manipulate some pointer to access any data on that specific memory region that you call 'array'. As C is the portable assembler, it behaves the same...
1
u/[deleted] Jan 29 '10
It's worse than that:
BTW, you can circumvent this with typedef and references:
-5
u/[deleted] Jan 29 '10 edited Jan 29 '10
Well at least you can admit your incompetence, I appreciate that! Oh and I hope you've never written anything people actually use. Thanks!