Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post-increment on a dereferenced pointer?

Trying to understand the behaviour of pointers in C, I was a little surprised by the following (example code below):

#include <stdio.h>  void add_one_v1(int *our_var_ptr) {     *our_var_ptr = *our_var_ptr +1; }  void add_one_v2(int *our_var_ptr) {     *our_var_ptr++; }  int main() {     int testvar;      testvar = 63;     add_one_v1(&(testvar));         /* Try first version of the function */     printf("%d\n", testvar);        /* Prints out 64                     */     printf("@ %p\n\n", &(testvar));      testvar = 63;     add_one_v2(&(testvar));         /* Try first version of the function */     printf("%d\n", testvar);        /* Prints 63 ?                       */     printf("@ %p\n", &(testvar));   /* Address remains identical         */ } 

Output:

64 @ 0xbf84c6b0  63 @ 0xbf84c6b0 

What exactly does the *our_var_ptr++ statement in the second function (add_one_v2) do since it's clearly not the same as *our_var_ptr = *our_var_ptr +1?

like image 339
ChristopheD Avatar asked May 13 '09 19:05

ChristopheD


People also ask

What happens when a pointer gets Dereferenced?

Dereferencing a pointer means getting the value that is stored in the memory location pointed by the pointer. The operator * is used to do this, and is called the dereferencing operator.

What does * p ++ do in C?

In C programming language, *p represents the value stored in a pointer. ++ is increment operator used in prefix and postfix expressions. * is dereference operator. Precedence of prefix ++ and * is same and both are right to left associative.

What is dereferenced pointer?

Dereference a pointer is used because of the following reasons: It can be used to access or manipulate the data stored at the memory location, which is pointed by the pointer. Any operation applied to the dereferenced pointer will directly affect the value of the variable that it points to.

What does the dereference operator (*) do?

In computer programming, a dereference operator, also known as an indirection operator, operates on a pointer variable. It returns the location value, or l-value in memory pointed to by the variable's value.


2 Answers

This is one of those little gotcha's that make C and C++ so much fun. If you want to bend your brain, figure out this one:

while (*dst++ = *src++) ; 

It's a string copy. The pointers keep getting incremented until a character with a value of zero is copied. Once you know why this trick works, you'll never forget how ++ works on pointers again.

P.S. You can always override the operator order with parentheses. The following will increment the value pointed at, rather than the pointer itself:

(*our_var_ptr)++; 
like image 100
Mark Ransom Avatar answered Sep 21 '22 01:09

Mark Ransom


Due to operator precedence rules and the fact that ++ is a postfix operator, add_one_v2() does dereference the pointer, but the ++ is actually being applied to the pointer itself. However, remember that C always uses pass-by-value: add_one_v2() is incrementing its local copy of the pointer, which will have no effect whatsoever on the value stored at that address.

As a test, replace add_one_v2() with these bits of code and see how the output is affected:

void add_one_v2(int *our_var_ptr) {     (*our_var_ptr)++;  // Now stores 64 }  void add_one_v2(int *our_var_ptr) {     *(our_var_ptr++);  // Increments the pointer, but this is a local                        // copy of the pointer, so it doesn't do anything. } 
like image 43
hbw Avatar answered Sep 23 '22 01:09

hbw