Why doesn’t the pointer increase the value?

Asked

Viewed 148 times

6

Here was to increase the value of the variable, but it does not work.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int *p, x = 10;

    p = &x;
    *p = (*p)++;
    printf("%d \n", *p);

    return 0;
}

Result: 10

  • The problem is your increment operator after the variable. If you do *p = ++(*p); you get the expected result.

  • Simple test: x = 3; y = ++x; // At this point x contains 4 and y contains 4 x = 3; y = x++; // At this point x contains 4 and y contains 3

2 answers

9


  • Where indefinite behavior occurs?

  • On the line I changed *p = (*p)++;.

  • "Where" was way of speaking, rsrs. What is the explanation for undefined behavior? Which rule was broken, shall we say so.

  • I can’t say exactly in this case. As far as I know the specification says nothing about (exactly) it. Maybe something related to this: https://en.wikipedia.org/wiki/Sequence_point. Maybe this helps explain: http://stackoverflow.com/a/949508/221800

  • I imagined something related to this, but I asked to see if you would explain didactically, because I don’t understand anything about Sequence points (only know they exist). If you discover more details, be sure to edit. :)

  • 1

    Also I have no deep knowledge about them. It’s been a long time since I promise to give a study, but always for later.

  • 1

    This does not work because it is using the operator ++ as post-increment. So much so that the line *p = (*p)++; for *p = ++(*p); will return the expected result. But why does this happen? the post increment operator is the last to be executed. The variable is even increased, but the value is lost because the operator = was used, thus taking the value before increment.

Show 2 more comments

2

*p = (*p)++; does not generate undefined behavior, as @Maniero said. This is a command with perfectly understandable and predictable behavior. The problem here is that the command (*p)++, in itself, already increases the value contained in x.

Note the code below:

int x = 0;
int *p = &x;

Here, the computer will create two "boxes" in memory.

  • A "little box" to hold an integer, called x.
  • And another "box" to keep the address of an integer, called p

in other words:

A variable of the type int stores an entire.

A variable of the type int* stores a address of a whole.

Now suppose x is at the address 0x000000 and that p is in 0x111111

The command (p) returns the address of x in memory, i.e., 0x000000

The command (*p) returns the value contained in the address 0x000000, that is to say, 0.

When you do *p = (*p)++; you are asking the processor to do the following:

*p = (*    p   )++;

 ^    ^    ^    ^

 4    2    1    3
  1. Take the address stored by p (0x000000)
  2. Access the value of this address (0)
  3. Increment that value (0 -> 1). Now x = 1.
  4. Assign the new value (1) à p.

In step 4 it gets serious. Here you are changing the contents of p, who was 0x000000, for 0x00001, that is 1 in hexadecimal. Doing so, p stops pointing to x, and starts pointing to a "foreign address". The edge of x remains 0X000000, but its content is now 1. The address of p also remains the same 0x111111 but your value, that before was 0x000000 (address x), now becomes (0x000001).

The code

int *p, x = 0;
p = &x;
*p = (*p)++;

can be rewritten as:

int x = 0;
int *p = 0x000000;

x = 1;
p = 0x00001;

assuming that the address of x be it 0x000000

  • 3

    It seems to me a case of undefined behavior yes. I could indicate in the specification of language something that states that the time of occurrence of the post-increment in relation to the assignment happens in the order that explanation describes?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.