Reputation: 139
let int * ptr,array[10]; ptr= array;
. Now each of the memory cells in array's contiguous locations have a fixed size. If the address of first cell is 1234 then the next cell must be at 1238 address. But we access it using a pointer as *(ptr+1)
.
I am confused regarding this. Any sources or answers? Thanks.
Upvotes: 0
Views: 238
Reputation: 5128
From the C11 standard: §6.5.2.1
A postfix expression followed by an expression in square brackets [] is a subscripted designation of an element of an array object. The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))). Because of the conversion rules that apply to the binary + operator, if E1 is an array object (equivalently, a pointer to the initial element of an array object) and E2 is an integer, E1[E2] designates the E2-th element of E1 (counting from zero).
E.G. what you're doing is basically what []
already does
Also (same standard) explains why pointers increment as you noticed: §6.5.6
When an expression that has integer type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the pointer operand points to an element of an array object, and the array is large enough, the result points to an element offset from the original element such that the difference of the subscripts of the resulting and original array elements equals the integer expression. In other words, if the expression P points to the i-th element of an array object, the expressions (P)+N (equivalently, N+(P)) and (P)-N (where N has the value n) point to, respectively, the i+n-th and i−n-th elements of the array object, provided they exist. Moreover, if the expression P points to the last element of an array object, the expression (P)+1 points one past the last element of the array object, and if the expression Q points one past the last element of an array object, the expression (Q)-1 points to the last element of the array object. If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined. If the result points one past the last element of the array object, it shall not be used as the operand of a unary * operator that is evaluated.
Upvotes: 5
Reputation: 40594
Pointer arithmetic is a different operation than normal arithmetic. In Pointer arithmetic, one argument has to be a pointer type, the other one has to be an integral type:
intA + intB
: Normal arithmetic, result is the sum.intA + pointerB
: Pointer arithmetic, the calculation performed is sizeof(*pointerB)*intA + pointerB
pointerA + intB
: Pointer arithmetic, the calculation performed is pointerA + sizeof(*pointerA)*intB
pointerA + pointerB
: IllegalpointerA[intB]
is just a shorthand for case 2 (pointerA + intB
), which is why it is equivalent to intB[pointerA]
(resolves to case 3), but that's only of use in the IOCCC.
Upvotes: 0
Reputation: 5315
My guess is that according to you (ptr + 1)
should give you 1235
but obviously it doesn't.
In addition to the answers given by others as to why it adds 4
and gives you 1238
, consider the case if it gave you 1235
, at 1235
you don't have an integer element(i.e. the 2nd element of the array).
But your pointer should point to an integer . Hence it doesn't work out this way.
Upvotes: 0
Reputation: 222274
The units of pointer arithmetic are pointed-to objects, not bytes.
If p
points to int
objects, then p+1
, p+2
, and p+3
point to successive int
objects, not to successive bytes. If p
points to a large structure, then they point to successive instances of the structure.
The compiler does its work behind the scenes to convert the pointer arithmetic into machine address arithmetic. So it multiplies offsets as necessary to convert from units of objects to units of bytes.
Upvotes: 0
Reputation: 61
The right operand of the + operator, lets call it x, is not the actual amount you're moving the pointer. Because int is 4 bytes, the compiler knows to actually skip to x*4.
Upvotes: 0
Reputation: 20503
Pointer arithmetic considers the size of the pointed type. For instance, if the value of ptr
is 1234
, since ptr
is of type int*
, then the value of p + 1
is 1234 + 1 * sizeof(int) == 1234 + 1 * 4 = 1238
(assuming sizeof(int) == 4
).
Upvotes: 1
Reputation: 46657
The compiler knows that ptr
is a pointer to a 4-byte type, so it knows that ptr+1
is 4 bytes further on in the memory.
If you think for a moment, it's clear that this must be so, as otherwise you couldn't write portable code without knowing the size of (for instance) an integer on your system.
Further, array indexing is exactly pointer arithmetic under the covers - that is, array[3]
is exactly the same as *(array + 3)
Upvotes: 3
Reputation: 5168
When indexing a pointer, the compiler knows that the index should be advanced by the size of the cell, in this case, a pointer of 4 bytes.
Upvotes: 1