2

So if an asterisk precedes the pointer name, it refers to the value of the address being pointed to

int anInteger = 30;
int* pointer = &anInteger;
*pointer;

While without having this operator preceding the pointer name, it refers to the value of the pointer itself, i.e. the address of what the pointer is pointing to

pointer;

(PLEASE correct me if I am wrong or if you just got some tips to share :) ) To me, this means that the above code can be translated to following with the assumption that the address of "myInteger" is 1234:

int* pointer = 1234;
30;

1234;

Now to what confuses me - Since that variables refer to locations in memory and names refer to the variables, it is odd to me that you do this when you want to change the value of a variable indirectly using a pointer:

*pointer = 15;

Because that could then be translated to 30 = 15, which does not look very valid..

Also, when you change the value of the variable directly, you do it like this:

anInteger = 33;

where "anInteger" would refer to the variable and thereby the memory address and because of that, I would translate this code to this:

1234 = 33;

SO, because of this, why don't you do this when assigning a new value to a variable indirectly using a pointer:

pointer = 33;

The exact question: Why do you write *pointername = 33; instead of pointername = 33?

  • 1
    The question is rather badly worded, which is understandable when the OP is confused. I don't see how this could be answered-as-asked... Or, to put it differently, *what exactly is the question*? Voting to close as unclear. Could also be "too broad" as you are, effectively, asking for another round through "what are variables, what are addresses, what are pointers, what is assignment"... (`1234 = newvalue;`? Oh, my...) – DevSolar Apr 27 '17 at 14:51
  • 6
    Welcome to Stack Overflow! Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Apr 27 '17 at 14:53
  • 1
    Your analogy drifted towards the end. `*pointer = 15` means the *memory location* becomes equal to `15` because `pointer` is the *address* of the *memory location* that happens to currently contain `30`. – Galik Apr 27 '17 at 14:57

4 Answers4

7

The exact question: Why do you write *pointername = newValue; instead of pointername = newValue?

These are two quite different things. (And you can do both.)

int i = 42;
int x = 23;
int * p = &i;

Now, I can change the content of the location I am pointing to...

*p = 123; // this changes the value of i

Or I can change the location I am pointing to.

p = &x; // now p points to x instead

The "natural" value of a pointer variable is a memory address. So, if I assign to the "naked" p, I am assigning a memory address (which I can obtain, for example, by using the & operator on a variable).

Using the operator *, I dereference the pointer variable. So, if I assign to the "combined" *p, I assign a value of the type pointed to (which ends up at the memory address contained in p).

Ladybug
  • 48
  • 8
DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • I distinctly remember how long it took *me* to wrap my head around pointers. I feel with you... – DevSolar Apr 27 '17 at 15:16
  • Thanks for the answer! However, in my question I explained the difference between using the asterisk and not to, which seems to be the thing that you are trying to explain.. Not sure though.. I still do not get it.. I mean if using the address-of operator returns the value pointed to, why can you then assign that value to something new? Wouldn't that just be like this: 23 = 54; and that does not look that valid to me? Thanks again and btw, I appreciate your answer but it feels like that you did not get my point :))))))))))))))))))))))))) –  Apr 27 '17 at 17:50
  • @Hei: Indeed I still do not get your point, which is why I asked, voted to close, and then answered "the exact question". And I think that stuff like "23 = 54" will just keep confusing you. Do not try to make connections like "but isn't that like...", as that will just lead you further astray. Your question would probably be much better answerable *without* the "as if" statements, which are confusing for novice and pro alike. – DevSolar Apr 27 '17 at 17:56
  • Yea the quick solution would probably just be not to think about it, but I like to know these sort of things. Anyway, you cannot assign a a value another value right? So why can you assign the value being pointed, another value? Like since you cannot do this: "23 = 30", I just cannot see how this should be valid "*pointer = 23".. Do you get my confusion? –  Apr 27 '17 at 18:05
  • @Hei: `*p` is equivalent to `i`. It is **not** equivalent to the *value* of `i`. You can assign to `*p` the same way you can assign to `i`. – DevSolar Apr 27 '17 at 18:11
  • Arghh now it is slowly starting to make sense! But is *p then equivalent to the memory address of i or how is the compiler understanding this? For instance, in this code `cout << *p;`, is the *p getting replaced with "i" does it get replaced with the memory address of i or what? Thanks again! It really means a lot. –  Apr 27 '17 at 18:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142849/discussion-between-devsolar-and-hei). – DevSolar Apr 27 '17 at 18:27
  • 1
    God. I really cannot thank you enough! It is unbelievable. No idea how to thank you enough. –  Apr 27 '17 at 20:35
1

How about this.

// i is the name of a memory location that now contains 5
int i = 5; 

// p is the name of a memory location that contains the address of
// the memory location called i
int* p = &i; 

// The memory location pointed to by p (which is also called i) now
// has the value 12 assigned to it
*p = 12;
Galik
  • 47,303
  • 4
  • 80
  • 117
  • Thanks! But if *p returns a value, how come it is valid to assign it another value? Wouldn't that just be like "5 = 12" which is not valid? –  Apr 27 '17 at 18:12
  • 1
    @Hei `*p` returns a *memory location* which is a *variable* and it *contains* a *value*. So `p` is where the memory is located and `*p` is the location itself. – Galik Apr 27 '17 at 18:46
1

Let's start with:

anInteger = 27;

rather than translating that to

1234 = 27

Let us write this as

[1234] := 27

which should be read as "the memory cell at address 1234 is assigned the value 27".

Now let us consider

pointer = &newValue;

Obviously (or perhaps not), we will translate that to

[1238] := 1242

which is read as "the memory cell at address 1238 is assigned the value 1242". (Where 1242 is the address of newValue)

Now we need a new way to write the statement where we don't want to alter the value in the pointer variable itself, instead we want to alter the variable that pointer is pointing to. We do that by prefixing with *:

*pointer = 17

Which gets translated into something like:

[[1246]] := 17

which we can read as "the memory cell whose address is given by the contents of memory cell 1246 is assigned the value 17".

The really hard thing to keep straight, is the difference between the value of the pointer, and the thing it is pointing at.

-2

Every spot of memory has two fields, the address field as well as the data field. For example, you may be at address 0x1234, but that address has a value as well

Address            Value

 0x1234    ||||||   0x1E

A variable knows about the address as well as the value. when you say

int var = 5;

Var has an address as well as a data field and the assignment operator says to replace the data field with 5.

Address            Value
 0x1234    ||||||   0x05

A *Pointer * on the other hand has a value of an address. when you write code like

int * varPtr = &var;

You are saying the Value of the varPtr variable is an address.

 Address            Value
 0x1357    ||||||   0x1234

When you "de-point" a pointer like *varPtr, you are going to the address of the value in the pointer and talking about it's value.

*varPtr would be 0x05

varPtr would be 0x1234

var is also 0x05

Ryan
  • 436
  • 5
  • 15