0

For example, this code below I just recently started working with pointers.

#include<stdio.h>
int main(){
    int x = 3;
    int y = 7;
    int *ip; //creating int pointer ip
    ip = &x; // ip storing the address of x
    printf("Address of x: %d\n", ip);
    *ip = 5;
    printf("Address of x: %d\n", ip);
    ip = &y; // ip also storing the address of y 
    printf("Address of y: %d\n", ip);
    *ip += 11;
    x += *ip;
    printf("%d\n", x); // value is 23
    printf("%d\n", y); // value is 18
    printf("%d\n", *ip); // value is 18
}

The code runs fine I wanna know why. I was thinking after the pointer start holding the address of the variable y the changes made using the pointer when it was pointing to x should be reverted since it no longer holds the address.

Carter Brown
  • 1
  • 1
  • 4
  • 1
    If you understand what `ip = (some address of some int)`, it explains why. Is that what you're getting hung up on ? – WhozCraig Jun 12 '19 at 00:32
  • 1
    "I wanna know why" - because you followed the rules of the language? Not sure what you are asking. (Except for incorrect format specifier, you should use `%p` instead of `%d`). – M.M Jun 12 '19 at 00:41
  • Did you think there was a flaw in the code? Where exactly? It looks correct to me. – Seva Alekseyev Jun 12 '19 at 00:48
  • A pointer is simply a normal variable that holds the *address of* something else as its value. In other words, a pointer *points to* the memory address where something else can be found. For example, while `int a = 5;` stores the immediate value `5` as its value, `int *b;` creates a pointer to `int`, and `b = &a;` stores the address of `a` as its value (the memory address where `5` is currectly stored). If you need the value stored at the memory address held by a pointer, you *dereference* the pointer using the unary `'*'` operator, e.g. `int c = *b;` will initialize `c = 5`). – David C. Rankin Jun 12 '19 at 00:51

2 Answers2

3

Can a single pointer point to two or more variables

No, a single pointer can only point to one thing. That thing could be an int, or an array (or an element of an array), or some other thing.

However, you can take a pointer and make it instead point to something else from then on! Like a different int, or a different element of an array.

Here you've accomplished that with the = assignment operator:

ip = &y;

Now it doesn't point to x any more; it points to y instead.


I was thinking after the pointer start holding the address of the variable y the changes made using the pointer when it was pointing to x should be reverted since it no longer holds the address.

That's a misunderstanding of what a pointer does. The pointer doesn't have any memory of the assignments you made through it. It doesn't even know about them. It's not a mask that temporarily smothers an existing object with a value that's only observable for the duration that the pointer relates to that object. It's a pointer. It points to things.

All it does is provide access to x (and later y). That's it. Period. Whatever you did to x (and later y) is between you and x (and later y). Rebinding the pointer later has absolutely no "undoing" effect on those actions.

Why does the code work? Because you followed the rules of the language and wrote a valid program.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • if the pointer is now pointing to y shouldn't the value of x be reverted to 3? – Carter Brown Jun 12 '19 at 00:51
  • @CarterBrown No why would it do that? – Lightness Races in Orbit Jun 12 '19 at 00:53
  • @CarterBrown It seems like you're perhaps imagining some permanent connection between the pointer, and the values you assigned to the `int` via that pointer. There isn't one. You used the pointer to access `x` then assigned a value to it. Then the program moved on to the next thing. There's no mechanism for the new value to be "undone", regardless of what happens with the pointer. It was just used to access `x`, that's all. – Lightness Races in Orbit Jun 12 '19 at 00:54
  • ok understand now, my initial problem was that we can make changes to the pointer and it would affect the value it's holding its address. but I understand now. – Carter Brown Jun 12 '19 at 01:05
  • @CarterBrown I added some words to this effect. – Lightness Races in Orbit Jun 12 '19 at 01:05
1

Pointer is a reference of a value, likes the room number as a room. int *ip; means ip is a room number.

ip = &x; means ip is the number of room x now. *ip = 5; means set 5 in the room which number is x, and x = 5; ip = &y; changes ip is the number of room y now.

It is clear that you take printf("x's address is %x, y's address is %x, ip is %x\n", &x, &y, ip) fellowed by ip = &x; and ip = &y; statements.

Wei Huang
  • 174
  • 1
  • 6