What does "* (pointer + integer)" do in C ++?

I am confused by what the line of code in this program does:

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 is a pointer), and cap is an argument.

In particular, I do not understand this line:

  *(temp + count) = *(foo + i); 

Why do pointers to parentheses exist and what will this do?

+6
source share
4 answers
 *(temp + count) = *(foo + i); 

The + operators perform pointer arithmetic. Adding an integer to the value of the pointer gives a new pointer, increasing the specified number of objects behind the original pointer. For example, if p is a pointer to arr[0] , then p+2 points to arr[2] .

The * operator removes the resulting pointer, giving you the object it points to.

In fact, the array indexing operator [] is defined in terms of pointer arithmetic, so A[i] means *(A+i) (ignoring operator overloading). Therefore, the above line of code:

 *(temp + count) = *(foo + i); 

can also be written (more clearly IMHO) as:

 temp[count] = foo[i]; 

You might want to read the comp.lang.c FAQ , specifically sections 4 (Pointers) and 6 (Arrays and Pointers). Most of the information is also applicable to C ++.

However, C ++ provides higher-level libraries, which can be more reliable than low-level C equivalents. In C ++, the idea of ​​writing code that is directly related to arrays and pointers to array elements rarely arises, unless the very low level of code and performance are critical and / or you are dealing with data structures from C code.

+9
source

In your example

 *(temp + count) = *(foo + i); 

equivalently

 temp[count] = foo[i]; 

The expression *(temp + count) adds the integer count to the temp pointer, which gives you a pointer to the element at the count position in the array pointed to by temp and dereferences it.

+2
source

*(temp + count) = *(foo + i) means:

get the temp address, add the offset count to it, then set the value in the resulting temp + count address to the value from the address foo with the offset i

Better would be:

 temp[count] = foo[i]; 

(By the way, this is the style of C (except for new ), not C ++. Very bad code.)

+1
source

Perhaps the key line of your question:

Why do pointers to parentheses exist [...]?

The * character in C and C ++ is somewhat vaguely semantically overloaded. You are right that sometimes it means "pointer to", as in int* int_ptr , where * means " int_ptr is a pointer to the type to the left of * ".

But it also means "dereference a pointer to ...", which actually happens. The expressions (temp + count) and (foo + i) are evaluated using pointers to int s, because adding an int type to a pointer value causes the pointer value to be shifted by an integer type (i.e. you calculate the memory address by some distance from the source pointer). Brackets simply ensure that add operations are performed prior to use * . Thus, *(temp + count) causes the value of the pointer to be dereferenced temp + count . This means that it evaluates the integer value stored at temp + count .

You can eliminate some confusion by thinking about the first value also indicating dereferencing: int* int_ptr equivalent to int *int_ptr , i.e. "if you are looking for int_ptr , you will get int ." This gives an intuitive idea of ​​why int* int_one, int_two declares int_one as a pointer to int and int_two as int - not as a pointer type.

0
source

All Articles