Pointers in college

Asked

Viewed 59 times

2

I’m studying pointers and this is a code used as an example that’s in the material given by the college. I went to replicate this code on my machine and the output is different from the material.
Code:

#include <stdio.h>

int main()
{
    int x=5, y=7, teste=21, aux=17;
    int *px, *py;

    px = &y;
    py = &x;

    printf("py - px = %d\n",py-px);
    printf("px = %u, *px=%d, &px = %d\n",px, *px, &px);
    printf("py = %u, *py=%d, &py = %d\n",py, *py, &py);
    px++;
    printf("px = %u, px=%d, &px = %d\n",px, *px, &px);

    py = px+3;
    printf("py = %u, *py=%d, &py = %d\n",py, *py, &py);
    printf("py - px = %d\n",py - px);

    return 0;
}

Just below is the output of the program on my machine:

py - px = -1<br>
px = 3782591132, *px=7, &px = -512376152
py = 3782591128, *py=5, &py = -512376144
px = 3782591136, *px=21, &px = -512376152
py = 3782591148, *py=32767, &py = -512376144
py - px = 3

I also use a IDE Online C to study and exit from the program is the same as this in college material.

Output in the Online IDE:

py - px = 1                                                                                                                                                               
px = 2271354488, *px=7, &px = -2023612824                                                                                                                                 
py = 2271354492, *py=5, &py = -2023612832                                                                                                                                 
px = 2271354492, *px=5, &px = -2023612824                                                                                                                                  
py = 2271354504, *py=593585370, &py = -2023612832                                                                                                                         
py - px = 3

According to the material, the pointer px points to the variable x and after the increment it should point to the variable y.

Yet on my machine, it’s pointing to the variable test. And on the line of subtraction between pointers px and py the result would be 1 and not -1.

I don’t know what could be wrong.

I am using Ubuntu 16.4.5 with gcc 5.4 and gcc 7.1 both with same output. And the Online IDE is RED HAT 7.1.1-3 with gcc 7.1.1.

  • You have no guarantee that x and y are consecutive. The compiler is free to put the desired order

1 answer

1

In the case presented the program will actually present different outputs on each machine, and sometimes at each execution, because the value stored in a pointer is a memory address. The problem is that because it is not an array, it may be that x and y are not contiguous positions in memory so incrementing x does not necessarily stop at y. (Remembering that in pointer arithmetic the sum and subtraction lead forwards to backwards in memory positions)

To illustrate what I am saying see the change I made in your code:

#include <stdio.h>

int main(){

    int array[] = {5, 7, 21, 17, 50};
    int *px, *py;

    px = array;
    py = px+1;

    printf("py - px = %d\n", py - px);
    printf("px = %u, *px=%d, &px = %d\n",px, *px, &px);
    printf("py = %u, *py=%d, &py = %d\n",py, *py, &py);
    px++;
    printf("px = %u, px=%d, &px = %d\n",px, *px, &px);

    py = px+3;
    printf("py = %u, *py=%d, &py = %d\n",py, *py, &py);
    printf("py - px = %d\n",py - px);

    return 0;
}

Obs.: I had to put one more position in the array because py+3 accessed a variable outside the array.

The output shown is as follows::

py - px = 1 // 1 espaço de diferença
px = 2865090736, *px=5, &px = -1429876568
py = 2865090740, *py=7, &py = -1429876576
px = 2865090740, px=7, &px = -1429876568
py = 2865090752, *py=50, &py = -1429876576
py - px = 3

Note now the positions in memory:

 x ->  0. 2865090736 // 2865090736 + 4*0
 y ->  1. 2865090740 // 2865090736 + 4*1
       2. 2865090744 // 2865090736 + 4*2
       3. 2865090748 // 2865090736 + 4*3
y+3->  4. 2865090752 // 2865090736 + 4*4

4 bytes is the space an int occupies.

Browser other questions tagged

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