Reputation: 493
In the code below could somebody explain why sum += *(ptr+i);
is the same as sum += ptr[i];
?
I understand that sum += *(ptr+i);
is dereferencing the memory address and then summing the numbers?
But in sum += ptr[i];
there is no deference...
#include <stdio.h>
#include <stdlib.h>
int main() {
int len, i, sum;
printf("Enter how many numbers you want to sum:");
scanf("%d", &len);
int* ptr;
ptr = (int*)malloc(sizeof(int) * len);
for(i=0; i < len; i++) {
printf("Enter a number:");
scanf("%d", ptr+i);
}
for(i=0; i < len; i++) {
//sum += *(ptr+i);
sum += ptr[i];
}
printf("The sum is %d", sum);
return 0;
}
Upvotes: 4
Views: 3499
Reputation: 19
sum += *(ptr+i);
here initially the index value "i" is added to the "ptr", then after dereferencing the array pointer to get the value. It is added to the "sum"
sum += ptr[i];
here directly accessing the value through its index value "i" from array "ptr". It is added to the "sum"
Upvotes: 0
Reputation: 726479
But in
sum += ptr[i]
there is no deference
There is no dereference only in the sense that the dereference operator *
is not used. However, dereference is still there, because index operator []
in C is an equivalent of *
: the two are completely interchangeable in all situations. When you see x[i]
you can rewrite it as *(x+i)
, and vice versa.
The ability to rewrite a dereference of an addition as index expression results in one of the least readable constructs in C grammar - an ability to rewrite x[i]
as i[x]
.
Upvotes: 0
Reputation: 310930
By the definition (the C Standard, 6.5.2.1 Array subscripting)
2 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).
Thus these three statements
sum += *(ptr+i);
sum += ptr[i];
sum += i[ptr];
are equivalent.
Take into account that the program has undefined behavior because the variable sum
was not initialized.:) To avoid an overflow it would be better to declare it like
long long int sum = 0;
// ...
printf("The sum is %lld\n", sum);
Upvotes: 3
Reputation: 4044
ptr[i]
evaluates to *(ptr + i)
Actual increment depends on the type of pointer
If ptr
is int
, an increment ie ptr + 1
will increment the address by 4(assuming int is 4 bytes) memory locations ie (if ptr
is 0xFF000005 ptr+1
will be 0xFF000009)
If ptr
is char
, an increment ie ptr + 1
will increment the address by 1 memory locations (if ptr
is 0xFF000005 ptr+1
will be 0xFF000006)
Upvotes: 0
Reputation: 399753
It's the same simply because that's how the language works. And yes, the []
indexing operator really does dereference the pointer to its left, it has to do that in order to get the value.
In C in general, a[b]
is equivalent to *(a + b)
.
This leads to the fun classic like
printf("%c\n", 4["foobar"]);
The above prints a
, since *(4 + "foobar")
is the same as *("foobar" + 4)
, i.e. "foobar"[4]
.
Upvotes: 1