0

Hey guys Im trying to get down some theoretical stuff for Pointers and Arrays. I was hoping someone could reaffirm some suspicions I had with the concept of pointers and arrays.

Suppose I had something like this

int ia[] = {0,1,2,3,4,5};
ia[2];  // =2

int* ip = &ia[0]; // pointer ip gets the address of element 0 in array ia
ip[2]; // ??
ip[2] = 42; // 

Most of this code is theoretical obviously but im a bit unsure of the last 2 lines. First off is saying ip[2] the same as saying ip now points to the 2ndth element in the array? Is it equivalent to saying *ip = ia[2] ?

Im also confused with the last line. ip[2] = 42; so the 2ndth element of the object that the ip points to, that address gets the value 42? Or is that an array notation method of dereferencing? Im just a bit confused on whats going on.

PresidentRFresh
  • 93
  • 1
  • 2
  • 10
  • `ip[2];` is a statement that has a value and does nothing with it. It's the same as saying `i;` `3;` or `main;`. AKA it does nothing. – Patashu Mar 21 '13 at 05:47
  • oh sorry I mean...how does it have the value 2 though? If ip originally points to the first element in the ia array then ip[2] value is 2...but logically im lost here... – PresidentRFresh Mar 21 '13 at 05:48
  • 1
    It is a statement that does nothing. It's like writing `;` - also a statement that does nothing. It is equivalent to not writing the line at all. It does nothing to anything by doing it. – Patashu Mar 21 '13 at 05:49
  • @Patashu, I don't think `main;` is valid (specifically due to main being special). Maybe replace it with `;` :) – chris Mar 21 '13 at 05:49
  • @chris Have you tried it? – Patashu Mar 21 '13 at 05:51
  • @Patashu, I was going from [this](http://stackoverflow.com/questions/15525613/is-it-illegal-to-take-address-of-main-function), which not all compilers warn equally about. I can't say whether this constitutes as using it, though. AFAIK, these are evaluated, so that's what made me suspicious. – chris Mar 21 '13 at 06:04
  • [This answer](http://stackoverflow.com/a/381549/1383051) explains your doubt concisely. – Anish Ramaswamy Mar 21 '13 at 06:08
  • @chris Oh, that counts as using it? I see. – Patashu Mar 21 '13 at 06:10
  • as u asked ,Is it equivalent to saying *ip = ia[2] ? no not same. But *(ip+2)==ia[2]; will be true(as u have assigned address of ia to ip so ia==ip). – linuxD Mar 21 '13 at 06:12
  • @Patashu, I'm not completely sure. It's not quite the same as the other question. Not really practical knowledge, but I would take comfort in knowing for sure. – chris Mar 21 '13 at 06:20
  • culprit is not initialized k only, as its some garbage but u use it as array index ie nothing but a memory location (assume k is -564564844), then it will happily do ++k but at k[++k]=..; it will fail due to illegal memory address, & result will be seg fault. – linuxD Mar 21 '13 at 06:31

4 Answers4

2

ia[n] is a different way of writing *(ia+n), therefore int* ip = &ia[0] is the same as int *ip = ia.

You are assigning ia to ip. That of course also makes ip[n] == ia[n] for all values of n.

Sebastian
  • 7,670
  • 5
  • 38
  • 50
1

When you write a[i] it is translated to *(a+i) where a is the base address (basically a pointer)

Also for arrays , base address is same as address of 1st element so address of a[0] is same as a [i.e a = &a[0]]

So, ip[2] = 42; translates to *(ip+2) = 42 Also, * gives value at that address ip[2] = 42 means *(ip+2) = 42 i.e value at location (ip+2)

Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59
1

The following creates an array called ia and stores the numbers in curly braces in the array:

int ia[] = {0,1,2,3,4,5};

The following in effect, does nothing:

ia[2];

However, if I take the above statement and change as follows, it assigns the value 2 to the integer t. This is because 2 is the third element in ia, and ia[2] references the third element in ia.

t = ia[2];

If your original declaration of ia had been

int ia[] = {0,1,15,3,4,5};

then you would be assigning t to 15.

The following changes the value at index 2 in ia to 42:

ip[2] = 42;

Now, ia consists of {0,1,42,3,4,5}.

This is because ia[2] is like saying *(ia + 2). If you assign ip to ia, then ip points to the first element of ia.

Similarly, ip[2] is like saying *(ip + 2). So, changing ip[2] will change ia[2], they reference the same chunk of memory.

2to1mux
  • 773
  • 5
  • 7
1

a[b] is exactly the same as *(a + b). ALWAYS. So ia[2] is equivalent to *(ia + 2), and ip[2] is equivalent to *(ip + 2).

However, it is more subtle than that because ia and ip have very different types. ia is of array type; whereas ip is of pointer type. So they are not "the same" -- they are not even the same type of thing. It's like apples and oranges.

An array expression, used within certain contexts, can be implicitly converted to a pointer to its first element. If you wrote, int *ip = ia;, that would be such an implicit conversion -- ip now points to the first element of ia. It is the exact same as what you wrote, int *ip = &ia[0];

When you write something like *(ia + 2), again, in this context the array is also implicitly converted to a pointer to its first element, kind of like *(&ia[0] + 2). Since we know that ip points to the first element of ia, when you use ia in any context where it gets converted to a pointer, it's the same as using the value of ip. That's why ia[2] and ip[2] work the same.

newacct
  • 119,665
  • 29
  • 163
  • 224