Reputation: 93
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.
Upvotes: 0
Views: 220
Reputation: 783
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.
Upvotes: 1
Reputation: 122429
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.
Upvotes: 1
Reputation: 7710
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
.
Upvotes: 2
Reputation: 5239
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)
Upvotes: 1