Jump to content
  • Advertisement
Sign in to follow this  
geekalert

is &table[0] same as table

This topic is 4439 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
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] ).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
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].

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!