I have a question about incrementing in pointers that I dont quite understand.
Lets see 2 small programs:
int iTuna=1;
int* pPointer= &iTuna;
*pPointer = *pPointer + 1 ; //Increment what pPointer is pointing to.
cout << iTuna << endl;
In this first program I increment what pPointer is pointing to like this "*pPointer = *pPointer +1".
And as I expected iTuna changed to "2" and the program printed out the value "2"
int iTuna=1;
int* pPointer= &iTuna;
*pPointer++; //Increment what pPointer is pointing to.
cout << iTuna << endl;
system("PAUSE");
return 0;
Here I incremented incremented what pPointer is pointing to this was "*pPointer++". But here iTuna stays as "1" and the programs prints out the value "1" .
Although I expected this one to work as the first, it didn't.
Please Help me and tell me why the second peice of code isn't working like I expected and how to get around it.
Thank You
*pPointer++;
is equivalent to
*pPointer;
pPointer++;
so it increments the pointer, not the dereferenced value.
You may see this from time to time in string copy implementations like
while(*source)
*target++ = *source++;
Since your problem is a matter of operator precedence, if you want to deref the pointer, and then increment, you can use parens:
(*pointer)++;
++ operator precedence is higher than *d dereference.
What you write is actually
*(p++)
However you should use
(*p)++
*ptr++; - increment pointer and dereference old pointer value
It's equivalent to:
*(ptr_p++) - increment pointer and dereference old pointer value
Here is how increment the value
(*ptr)++; - increment value
That's becuase ++ has greater precedence than *, but you can control the precedence using ()
In the Second program you are not increasing the the content at the pPointer address, but you are increasing the pointer. So suppose here if the pPointer value(memmory location allocated to iTuna) is 1000 then it will increase the location to 1000+2(int size)=1002 not the content to 1+1=2. And In the above program you are accessing the pointer location contents. Thats why you are not getting the expected results
*pPointer++; - Here dereference operator(*) has more precedence than increment operator(++). So this statement is first dereferencing and incrementing the pointer. After this you are printing the value of iTuna which will give you the same value. You are not printing the value by dereferencing pointer variable(*pPointer), because this will leads to crash(undefined behaviour). Because pPointer is now incremented.
Use like (*pPointer)++; to increment the value which is pointed by pPointer.
To get clear idea print the address stored in pPointer variable before and after your increment statement.
In the first case, the content of the pointer is incremented because *pPointer correspond to the content of the variable iTuna.
In the second one, the content is not incremented because you pPointer incrementing the pointer address. Remembering operator precedence rules, postfix operators such as increment (++) and decrement (--), have higher precedence than prefix operators, such as the dereference operator (*). Therefore, writting
*pPointer++
is equivalent to
*(pPointer++)
And what it does is to increase the value of pPoiner (so it now points to the next element), but because ++ is used as postfix, the whole expression is evaluated as the value pointed originally by the pointer (the address it pointed to before being incremented).
The correct code to have what you are expecting for is the following:
++*pPointer
or
(*pPointer)++
Related
UPDATE: I think I have done well in my exam; thanks for everyone's help and hope you have a nice week(end)!
I am currently revising pointers for my C++ exam and came across an example that I still cannot comprehend.
Consider the following (partial) code:
int x = 17;
int *ptr = &x;
cout<<*&ptr<<" "<<&ptr<<" "<<ptr<<" "<<*ptr<<endl;
I understand that &ptr and ptr are the same, i.e. that print the address of variable x. I also understand that *ptr prints out the value inside variable x, i.e. 17.
However, I cannot understand the first one, i.e. *&ptr. It prints an address, but I do not know to which it points to. I have tried researching it, but I did not find much on it. What does the *& combination represent?
ptr and &ptr are not the same. &ptr is the address of ptr (i.e., a pointer to a pointer).
* is the dereference operator - it returns the value the pointer points to. So, if you dereference &ptr, you're asking for "the value that the address of ptr is pointing to", or ptr (which, of course, is in turn, the address of x).
in *&ptr expression, operator precedence is the same for * and for & (but precedence is from right to left). So it just takes address of ptr (stack address), but then dereferences it, basically that is equivalent to just ptr.
You have (fantasy memory addresses):
variable value address
x 17 0x0001
ptr == &x 0x0001 0x0002
* dereferences a pointer. For example *ptr uses the value of ptr which is 0x0001 and looks what value it find at that address: 17.
& on the other hand is the address-of operator. &x is the address of x, it is 0x0001. The address of x is ptr.
&*ptr is just ptr.
I understand that &ptr and ptr are the same ...
No they are not. ptr is the address of x (0x0001). &ptr is the address of ptr (0x0002).
One thing that is maybe not so clear from other answers: in an expression like *&ptr, the dereference operator * does the inverse operation of the "address_of" operator &, so that they effectively cancel out when combined, and the result is that *&ptr is identical to ptr.
A simple way to think about this is to consider the operation of halving a number (i.e. dividing it by 2) and doubling it (multiplying it by 2). Take any number x: if you halve it and then double the result, what do you get? You get x back, of course, because those two operations cancel out. You are starting from a value, applying a function to it, and then applying its inverse function, so that you end up with the original value.
& and * work the same way: you have a variable ptr with its value, you take the address of that variable with &, and then * takes that address and gives you the value that is stored there, which is the value of ptr.
This means that *&ptr is identical to ptr, and you can do this *& trick it as many times as you want: it would be the same if you wrote *&*&*&*&*&ptr. In case you are wondering, all these operators are evaluated from right to left, because that's what the table of precedence and associativity says (they have the same precedence, 2, and when you have multiple operators with the same precedence you apply them in the order specified by the associativity).
I'm trying to understand what the difference between these two functions is:
void funk1(char* goal, char* source){
int i = 0;
while((goal[i]= source[i]) != ’\0’)
i++; }
and
void funk2(char* goal, char* source){
while((*goal= *source) != ’\0’){
goal++;
source++;
} }
Can someone help me please?
Whats the difference between incrementing a pointer compared to incrementing the elements of the array the pointer points to?
Incrementing a pointer (or more generally, any iterator) modifies the pointer. The resulting pointer will point to the next element of the array.
Indirecting through a pointer and incrementing the pointed object modifies the pointed object. How increment modifies the object depends on the type of the object.
Note that in neither of your examples do you increment element of an array. In the first you increment the variable i which is an integer that you use as an index, while in the second you increment two pointers.
The compiler will probably create the same executable for both cases. But if you will compile with explicit flag that tells the compiler "Do not optimize", funk2 is a bit faster (note that on todays modern computer it is neglect).
Why?
funk1 requires 4 register while funk2 requires only 3.
Because the operation goal[i] is equivalent to *(goal + i), which is a heavier computation than ++.
goal[i] is equivalent to *(goal + i), so both implementations work.
Note: none of these functions are actually "incrementing the elements of the array the pointer points to". The elements of the array are modified with operator=, but the ++ just modifies the pointers themselves.
What is the purpose of the parentheses in the following piece of code? What would be happening if the parentheses were not there? Sorry, I am new to C++.
void foo(int * xPtr) {
(*xPtr)++;
}
With the parentheses, you increment the object the pointer is referencing, without them, you are incrementing the pointer to the next memory address and THEN dereferencing it (which in this case would do nothing).
If parentheses are not there then you are basically modifying the memory address stored inside xPtr and then dereferencing the pointer. So clearly this is not what is desired.
Say we want the value stored at memory location 1000 to be incremented, but
*xPtr++;
results in incrementing the memory location by 4 and then,value at memory location 1004 is accessed, assuming size of integer on the machine is 4 bytes.
(*xPtr)++ will increment the thing xPtr points to.
Postfix ++ has higher precedence than unary *; without parentheses, the code would be parsed as *(xPtr++) - you're incrementing xPtr, not the thing xPtr points to.
I have a question about incrementing in pointers that I dont quite understand.
Lets see 2 small programs:
int iTuna=1;
int* pPointer= &iTuna;
*pPointer = *pPointer + 1 ; //Increment what pPointer is pointing to.
cout << iTuna << endl;
In this first program I increment what pPointer is pointing to like this "*pPointer = *pPointer +1".
And as I expected iTuna changed to "2" and the program printed out the value "2"
int iTuna=1;
int* pPointer= &iTuna;
*pPointer++; //Increment what pPointer is pointing to.
cout << iTuna << endl;
system("PAUSE");
return 0;
Here I incremented incremented what pPointer is pointing to this was "*pPointer++". But here iTuna stays as "1" and the programs prints out the value "1" .
Although I expected this one to work as the first, it didn't.
Please Help me and tell me why the second peice of code isn't working like I expected and how to get around it.
Thank You
*pPointer++;
is equivalent to
*pPointer;
pPointer++;
so it increments the pointer, not the dereferenced value.
You may see this from time to time in string copy implementations like
while(*source)
*target++ = *source++;
Since your problem is a matter of operator precedence, if you want to deref the pointer, and then increment, you can use parens:
(*pointer)++;
++ operator precedence is higher than *d dereference.
What you write is actually
*(p++)
However you should use
(*p)++
*ptr++; - increment pointer and dereference old pointer value
It's equivalent to:
*(ptr_p++) - increment pointer and dereference old pointer value
Here is how increment the value
(*ptr)++; - increment value
That's becuase ++ has greater precedence than *, but you can control the precedence using ()
In the Second program you are not increasing the the content at the pPointer address, but you are increasing the pointer. So suppose here if the pPointer value(memmory location allocated to iTuna) is 1000 then it will increase the location to 1000+2(int size)=1002 not the content to 1+1=2. And In the above program you are accessing the pointer location contents. Thats why you are not getting the expected results
*pPointer++; - Here dereference operator(*) has more precedence than increment operator(++). So this statement is first dereferencing and incrementing the pointer. After this you are printing the value of iTuna which will give you the same value. You are not printing the value by dereferencing pointer variable(*pPointer), because this will leads to crash(undefined behaviour). Because pPointer is now incremented.
Use like (*pPointer)++; to increment the value which is pointed by pPointer.
To get clear idea print the address stored in pPointer variable before and after your increment statement.
In the first case, the content of the pointer is incremented because *pPointer correspond to the content of the variable iTuna.
In the second one, the content is not incremented because you pPointer incrementing the pointer address. Remembering operator precedence rules, postfix operators such as increment (++) and decrement (--), have higher precedence than prefix operators, such as the dereference operator (*). Therefore, writting
*pPointer++
is equivalent to
*(pPointer++)
And what it does is to increase the value of pPoiner (so it now points to the next element), but because ++ is used as postfix, the whole expression is evaluated as the value pointed originally by the pointer (the address it pointed to before being incremented).
The correct code to have what you are expecting for is the following:
++*pPointer
or
(*pPointer)++
I'm implementing a template pointer wrapper similar in functionaltiy to boost::shared_ptr.
I have a pointer to an integer ptrInt.
What I want to do: Increment the integer ptrInt points to.
My initial code was this: *ptrInt ++;, although I also tried the same using (*ptrInt) ++;
Apparently, however, this doesn't seem to do what I expected it to.
In the end I got it working using *ptrInt += 1;, however I'm asking myself:
What exactly does *ptrInt ++; do?
Is there a more elegant solution to using *ptrInt += 1;?
*p++ // Return value of object that p points to and then increment the pointer
(*p)++ // As above, but increment the object afterwards, not the pointer
*p += 1 // Add one to the object p points to
The final two both increment the object, so I'm not sure why you didn't think it worked. If the expression is used as a value, the final form will return the value after being incremented but the others return the value before.
x = (*p)++; // original value in x
(*p)++;
x = *p; // new value in X
or
x = ++*p; // increment object and store new value in x
*ptr++ equivalent to *(ptr++)
*ptrInt ++ will
increment ptrInt by 1
dereference old value of ptrInt
and (*ptrInt) ++ as well as *ptrInt += 1 will do what you want.
See Operator Precedence.
(*ptr)++ should do it, unless you are using its value right away. Use ++*ptr then, which is equivalent to ++(*ptr) due to right-to-left associativity.
On a side note, here is my smart pointer implementation, perhaps it can help you in writing your own.
The expression is evaluated using the rules for operator precedence.
The postfix version of ++ (the one where ++ comes after the variable) has a higher precedence than the * operator (indirection).
That is why *ptr++ is equivalent to *(ptr++).
You want (*ptrInt)++, which increments the object, which generally does the same as *ptrInt += 1. Maybe you have overloaded the += operator, but not the ++ operators (postfix and prefix increment operators)?
The more elegant way is whatever you tried before. i.e (*ptrInt) ++;
Since, you aren't satisfied with that, it might be because of the post - increment.
Say, std::cout<<(*ptrInt) ++; would have shown you the un-incremented value.
So, try giving ++(*ptrInt); which might behave as you expected.
*ptrInt++ in your case does increment the pointer, nothing more (before that it fetches the value from the location at throws it away. of course if you use it in a more complex expression it will use it)
(*ptrInt)++ does what you are looking for.