Incrementing Pointers
Asked Answered
U

6

16

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

Unbar answered 1/8, 2012 at 7:24 Comment(4)
Be careful of the operator precedence of the two operators you use.Giddings
In the second one, you incrementing the pointer address. So if you do cout << pPointer; before and after incrementing you will get different value.Aroma
You have to dereference first, and then increment what's pointed to - ++*pPointer.Beautician
And, BTW, using a p prefix on something named Pointer is rather silly. What could Pointer be, if not a pointer?Beautician
J
30
*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)++;
Jarrod answered 1/8, 2012 at 7:28 Comment(6)
Can you explain why? And tell me how I can get to use ++ operator for incrementing what pPointer is pointing toUnbar
This will increment what the pointer is pointing to: (*pPointer)++;Trinary
(*pPointer)++ or *pPointer += 1Oberland
@MohamedAhmedNabil : pPointer is a memory case it contain an adress of a memory case that contains an integer. if you want to increment the integer, you have first to get the adress of that integer, this adress is stored in the memory case (pPointer). so to increment the the integer, you have to increment the content of the adress stored in pPointer : (*pPointer)++ not the adress it self (pPointer++). *pPointer++ increments pPointer because ++ is priority than * operator.Oberland
Thanks alot for your help, this is by far the best answer. But, I need to ask you one more thing. What does dereference meanUnbar
@MohamedAhmedNabil #4955698Jarrod
N
6

++ operator precedence is higher than *d dereference.

What you write is actually

*(p++)

However you should use

(*p)++
Neill answered 1/8, 2012 at 7:29 Comment(0)
H
3
 *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 ()

Heda answered 1/8, 2012 at 7:29 Comment(0)
E
1

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

Erechtheus answered 1/8, 2012 at 7:35 Comment(1)
In c++ it is of 2 bytes=16 bits.Erechtheus
C
0

*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.

Cristycriswell answered 1/8, 2012 at 15:30 Comment(0)
R
0

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)++

Renae answered 12/8, 2019 at 5:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.