Reputation: 109
I'm confused as to what a line of code does in this program:
int *temp = new int [cap];
int num = 0;
for(int i = name; i < number; i++)
{
*(temp + count) = *(foo + i);
num++;
}
name
, number
, and foo
are global variables (foo
being a pointer),
and cap
is an argument.
Specifically, I don't understand this line:
*(temp + count) = *(foo + i);
Why are there pointers to the parentheses, and what will this do?
Upvotes: 6
Views: 153
Reputation: 55524
In your example
*(temp + count) = *(foo + i);
is equivalent to
temp[count] = foo[i];
The expression *(temp + count)
adds integer count
to pointer temp
, which gives you a pointer to element at position count
in the array pointed to by temp
and dereferences it.
Upvotes: 2
Reputation: 16499
Perhaps the key line of your question is:
Why are there pointers to the parentheses [...] ?
The *
symbol in C and C++ is somewhat confusingly semantically overloaded. You are correct that it sometimes means "pointer to," as in int* int_ptr
, where the *
means "int_ptr
is a pointer to the type to the left of the *
".
But it also means "dereference a pointer to...", which is what's actually going on here. The expressions (temp + count)
and (foo + i)
evaluate to pointers-to-int
s, because adding an int-like type to a pointer value results in that pointer value offset by the integer type (i.e., you are calculating the memory address at some distance from the original pointer). The parentheses simply ensure that the addition operations happen before the *
's are applied. So *(temp + count)
is dereferencing the pointer value temp + count
. This means that it evaluates to the integer value stored at the address temp + count
.
You can eliminate some of the confusion by thinking of the first meaning as also indicating a dereference: int* int_ptr
is equivalent to int *int_ptr
, i.e., "if you dereference int_ptr
, you'll get an int
." This gives an intuitive sense of why int* int_one, int_two
declares int_one
as a pointer to an int
, and int_two
as an int
-- not as a pointer type.
Upvotes: 0
Reputation: 263197
*(temp + count) = *(foo + i);
The +
operators are performing pointer arithmetic. Adding an integer to a pointer value yields a new pointer incremented a specified number of objects past the original pointer. For example, if p
is a pointer to arr[0]
, then p+2
points to arr[2]
.
The *
operator deferences the resulting pointer, giving you the object that it points to.
In fact, the array indexing operator []
is defined in terms of pointer arithmetic, so that A[i]
means *(A+i)
(ignoring operator overloading). So the above line of code:
*(temp + count) = *(foo + i);
could also be written (more clearly IMHO) as:
temp[count] = foo[i];
You might want to read the comp.lang.c FAQ, particularly sections 4 (Pointers) and 6 (Arrays and Pointers). Most of the information is also applicable to C++.
However, C++ provides higher-level library interfaces that can be more robust than the low-level C equivalents. It's seldom a good idea in C++ to write code that deals directly with arrays and pointers to array elements, unless it's very low-level code and performance is critical and/or you're dealing with data structures from C code.
Upvotes: 9
Reputation: 8401
*(temp + count) = *(foo + i)
means:
get the temp
address, add count
offset to it, then set value in resulting address temp + count
to value from address foo
with offset i
Better will be:
temp[count] = foo[i];
(By the way, it's a C style (except for new
), not C++. Very bad code.)
Upvotes: 1