Why does not the pointer increase the value?

5

Here was to increment 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

    
asked by anonymous 28.07.2016 / 03:46

2 answers

6
___ erkimt ___ Why does not the pointer increase the value? ______ qstntxt ___

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

#include <stdio.h>

int main() {
    int *p, x = 10;
    p = &x;
    (*p)++;
    printf("%d \n", *p);
    return 0;
}

Result: 10

    
______ ___ azszpr143087

This produces undefined behavior. The increment operator is already an assignment operator. Just leave it alone everything works.

#include <stdio.h>

int main() {
    int *p, x = 10;
    p = &x;
    (*p)++;
    printf("%d \n", *p);
    return 0;
}

See working in ideone and in CodingGround .

    
______ azszpr143228 ___

%code% does not generate undefined behavior, as @bigown said. This is a command with perfectly understandable and predictable behavior. The problem here is that the command %code% already increments the value contained in %code% .

Note the code below:

%pre%

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

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

In other words:

A variable of type %code% stores an integer.

A variable of type %code% stores an address of an integer.

Now suppose that %code% is at address %code% and that %code% is in %code%

The command %code% returns the address of x in memory, that is, %code%

The command %code% returns the value contained in the address %code% , that is, %code% .

When you do %code% you are asking the processor to do the following:

%pre%
  • Get the address stored by %code% ( %code% )
  • Access the value of this address ( %code% )
  • Increase this value ( %code% -> %code% ). Now %code% .
  • Assign the new value ( %code% ) to %code% .
  • In step 4, things get serious. Here you are swapping the content of %code% , which was %code% , to %code% , which is %code% to %code% . Doing this, %code% stops pointing to x, and starts pointing to an "outside address." The address of x remains %code% , but its content is now %code% . The address of %code% also remains the same %code% but its value , which was previously %code% (x address), now becomes% (co_de%). >

    The code

    %pre%

    can be rewritten as:

    %pre%

    Assuming the address of %code% is %code%

        
    ___
    28.07.2016 / 03:57
    1

    *p = (*p)++; does not generate undefined behavior, as @bigown said. This is a command with perfectly understandable and predictable behavior. The problem here is that the command (*p)++ already increments 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 "box" to store an integer, called x.
    • And another "box" to store the address of an integer, called p

    In other words:

    A variable of type int stores an integer.

    A variable of type int* stores an address of an integer.

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

    The command (p) returns the address of x in memory, that is, 0x000000

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

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

    *p = (*    p   )++;
    
     ^    ^    ^    ^
    
     4    2    1    3
    
  • Get the address stored by p ( 0x000000 )
  • Access the value of this address ( 0 )
  • Increase this value ( 0 -> 1 ). Now x = 1 .
  • Assign the new value ( 1 ) to p .
  • In step 4, things get serious. Here you are swapping the content of p , which was 0x000000 , to 0x00001 , which is 1 to hexadecimal . Doing this, p stops pointing to x, and starts pointing to an "outside address." The address of x remains 0X000000 , but its content is now 1 . The address of p also remains the same 0x111111 but its value , which was previously 0x000000 (x address), now becomes% (co_de%). >

    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 the address of 0x000001 is x

        
    28.07.2016 / 20:08