is &table[0] same as table

Started by
13 comments, last by Fruny 17 years, 11 months ago
if i define it as int table[32]; (there is a question, look at the title)
A JPEG is worth a thousand and twenty four DWORD's. ;)
Note: Due to vacationing my website will not be updated till late-August. Of course I still have internet, but who wants to program during a vacation?
Advertisement
table would be an array, while &table[0] will return a pointer to an integer (a pointer to the first element of the array).

Difference between an array and a pointer.

I think the two are syntatically interchangable in most cases though.
[WRONG]
I believe so.
"Table" is really the address of the first element so they are both equal.
[\WRONG]
Nope.

Table can be used to get the value of &table[0], so in a sense yes, but sizeof( table ) != sizeof( &table[0] ).

Also, the address value obtained from table == &table[0] meaning that they point to "the same address", yet are different types (int * and int (*)[32] ).
An additional subtlety arises when table is an object with an overloaded operator[](), such as a std::vector, std::string, std::deque, std::map, std::valarray or std::bitset; which, I agree, is not the case in your example.

This is particularly controversial in the case of std::vector<bool>, for which operator[]() does not return a reference to a bool, unlike all the other versions of that class template.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Aren't they exactly the same thing? table is also the adress to the first element..
Quote:Original post by password
Aren't they exactly the same thing? table is also the adress to the first element..

If you had bothered reading the other posts, you would know that isn't the case. table is an array of 32 integers in the example given, but can be used to get the address to the first element. This, however, doesn't mean table IS the address to the first element, because it isn't.
Yes and no, they both point to the same spot but are different types. The OP defined table as int table[32] so table's type is int * [32] while &table[0]'s type is int *. So basically using one instead of the other would cause the compiler to complain.
Quote:Original post by Brother Bob
Quote:Original post by password
Aren't they exactly the same thing? table is also the adress to the first element..

If you had bothered reading the other posts, you would know that isn't the case. table is an array of 32 integers in the example given, but can be used to get the address to the first element. This, however, doesn't mean table IS the address to the first element, because it isn't.


I was talking about what I have read in a book myself, also I DID read the other posts so don't atleast tell me, what I did when you don't even know. I was merely pointing out that they had the same adress and that's a fact, the other was a question and the answer is that they are different types.

If you're going to correct me, and in an irascible way at that, do it right atleast..
Quote:Original post by password
I was merely pointing out that they had the same adress and that's a fact, the other was a question and the answer is that they are different types.


The problem is that table itself doesn't have an address (try doing &table). It is not a variable: table[0], table[1]... are. However, table has a value, which is indeed the address of table[0].

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement