Reputation: 361
I've read in the C99 standard that that is how arrays are expanded, but I can't wrap my head around why that would work. If I have
a[5]
and a is an integer, then wouldn't that be accessing the second element and not the fifth (assuming integers are four bytes in size)? Shouldn't it expand to
*(a+5*sizeof(a))
since each byte gets its own address?
Upvotes: 2
Views: 138
Reputation: 31
Its been a long time since I actually coded in C so forgive me if I make a mistake in the syntax or function calls.
An array is simply like a linear table of elements (strings, ints, floats, chars, structs, etc), a[5] is telling the compiler to get to the 6th element in that "table" regardless of what type it is. The compiler knows how many memory locations to jump based on the type the array variable was declared as.
Now, when you are pointing to memory, you retrieving the content by using a pointer to the location in memory where you want to start, in the case of an array of integers, you need to tell it how many memory locations each integer is in order to jump and get the data from the represented array element.
With this in mind, lets supposed that "a" represents an array in memory that has been allocated with integers as elements, in this case, if you do:
int *a = malloc(10 * sizeof(int));
/* initialize a with values */
/* .... some time later retrieve the 6th element: a[5] */
int x = *(a + 5);
The program will first jump over 5 "a" elements, which are as we know integers, by doing:
sixth_element_address = a+5*sizeof(a)
then retrieves the contents and assigns it to an integer variable:
x = *(sixth_element_address)
So using array notation is the same as doing it with pointer notation as you see.
Now, the only reason I would use pointer notation is when I do not have declared an array variable itself.
Upvotes: 0
Reputation: 123488
Pointer arithmetic is based on the size of the pointed-to type; if you have a pointer p
to a type T
, then p+1
gives you the address of the next instance of that type. So, given something like the following:
char *pc = 0x1000;
int *ic = 0x1000;
double *dc = 0x1000;
pc + 1
gives the address of the next char
object (which will be at 0x1001
), ic + 1
gives the address of the next int
object (which will be at 0x1004
for a four-byte int
), and dc + 1
gives the address of the next double
object (which will be at 0x1008
for an 8-byte double
).
This is why a[i]
is defined as *(a + i)
- you're accessing the i
'th element of the array, not the i
'th byte of the array.
Upvotes: 10
Reputation: 36401
This is pointer arithmetic. A[I]
is exactly *(A+I)
. Now, what A+I
means? It is the address of I
-th element of A
. If A
is an array of int
s then A+5
is the address of the 6-th element of the array A
.
When you measure in bytes, the value of the address is the value of the address of A
plus 5*sizeof(int)
in bytes.
Upvotes: 2
Reputation: 473
Each element in an array of ints, holds a sizeof(int) space to store the number. So technically your array is a sizeof(int)*size.
Upvotes: 0