1

I'm having trouble understanding what the output would be

int main( ) {
        int x = 5, y = 10, z = 20;
        int *ptr1 = &x, *ptr2 = &y, *ptr3 = &z;
        *ptr2 = *ptr3 + *ptr1;
        ptr2 = ptr1;
        *ptr2 = *ptr3;
        printf("%d and %d and %d\n", x,y,z);

        /*char str[] = "Stackoverflow is kind.";
        int len = strlen(str);
        printf("%s and %d\n", str, len);

        char *p;
        p = str;
        printf("%c and %c and %c and %c\n",
                   *p, str[3], *(p+9), str[len-2]);*/
    return 0;
}

Would the first addition line even be allowed? I thought you couldn't add pointers. And what would the difference be between these 2 lines?

ptr2 = ptr1;
*ptr2 = *ptr3;

Obviously they are different pointers, but how do they function differently?

I've ran the program and got 20 25 20 but I don't understand how

KevinDTimm
  • 14,226
  • 3
  • 42
  • 60
Unborn
  • 33
  • 2
  • 2
    What is "prblm"? Can you use normal English? – too honest for this site Oct 14 '16 at 17:36
  • @Olaf - apparently `problem` cannot be used in a title and the OP found that workaround. The workaround is gone :) – KevinDTimm Oct 14 '16 at 18:12
  • @KevinDTimm Too bad he did the wrong conclusion from this. (What is the error-message one gets?) – too honest for this site Oct 14 '16 at 19:15
  • 1
    @Olaf Instead of asking if someone can use normal English, why not correct it? That is beating a dead horse, as far as I'm concerned. @Unborn `Obviously they are different pointers, but how do they function differently?` Clearly you are not well-acquainted with pointers: It would benefit you to learn about them first, https://www.tutorialspoint.com/cprogramming/c_pointers.htm –  Oct 14 '16 at 19:17
  • @Olaf - "These words are not allowed in titles: problem" – KevinDTimm Oct 14 '16 at 19:19
  • 1
    @ThyArtIsCode: Believe it or not: I was not aware this word is not allowed. So I assumed OP used street slang, which makes a correcting like feeding fish. I prefer to teach fishing instead. But come to thought, it makes sense for a site dedicated to programming problems not to allow such redundancy. – too honest for this site Oct 14 '16 at 20:05
  • @KevinDTimm: Well, my conclusion there is a deeper reason they are not allowed might not be obvious to everyone then. I thought they'd state it a bit more clear. – too honest for this site Oct 14 '16 at 20:08

3 Answers3

2

Here you assign addresses of x y and z to those three pointers:

int *ptr1 = &x, *ptr2 = &y, *ptr3 = &z;

The operator * is used to deference the pointer. 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.

So you basically sum value 1 and 3 here:

*ptr2 = *ptr3 + *ptr1;
*ptr2 = 20 + 5 -> 25

Than, you make ptr2 point to the same address as ptr1, so they both point to the same value, which is 5:

ptr2 = ptr1;

Lastly, you change value that ptr2 points to same as ptr3 points to so it becomes 20. But remember ptr2 and ptr1 point to the same address so value at both ptr1 and ptr2 is 20 now:

*ptr2 = *ptr3;
Community
  • 1
  • 1
Anton Kim
  • 879
  • 13
  • 36
1

After executing the lines

int x = 5, y = 10, z = 20;
int *ptr1 = &x, *ptr2 = &y, *ptr3 = &z;

the following conditions are true:

 ptr1  == &x
*ptr1  ==  x ==  5
 ptr2  == &y
*ptr2  ==  y == 10
 ptr3  == &z
*ptr3  ==  z == 20

Thus, the expression *ptr1 is the same as the expression x, *ptr2 is the same as y, etc. So the line

*ptr2 = *ptr3 + *ptr1;

isn't adding the pointer values, it's adding the values of the objects the pointers point to - IOW, this is equivalent to writing

y = x + z;

In the line

ptr2 = ptr1;

you're setting ptr2 to same value as ptr1, meaning it will point to the same object as ptr1. After this line, the following conditions are all true:

 ptr2 ==  ptr1 == &x
*ptr2 == *ptr1 ==  x == 10

Finally, the line

*ptr2 = *ptr3;

sets the value of the object that ptr2 points to (x) to the value of the object that ptr3 points to (z); IOW, this is equivalent to writing

x = z;

So, over the course of the program:

y = z + x == 25
x = z == 20

hence your output.

John Bode
  • 119,563
  • 19
  • 122
  • 198
0

In this line:

*ptr2 = *ptr3 + *ptr1;

You're not adding pointers. You're adding what the pointers point to. In this case, it's the same as y = z + x.

ptr2 is pointer of type int *, while *ptr2 is of type int.

dbush
  • 205,898
  • 23
  • 218
  • 273