Reputation: 810
So I am trying to understand pointers and references.
Lets say we have the following:
int val = 100;
int *ip = &val;
Why is that not the same as simply pointing to val like this:
int val = 100;
int *ip = val;
Upvotes: 2
Views: 410
Reputation: 125455
Easy way to understand why they are not the-same if you do this your self.
int val = 100;
cout<<val<<endl;
You shoud get 100 as output.
Now try:
int val = 100;
cout<<&val<<endl;
You should get a 0x7ffc201cab0c or something similar as the outout.
If you access val
with the "&", you are accessing the memory address of it.
If you access val
without the "&", you are accessing the value of it.
Therefore if you do
int val = 100;
int *ip = val;
you will should get a warning or an error but assuming that you want to see it compile so you can figure out what would happen, this is what it you should do:
int val = 100;
int *ip;
*ip = val;
This is the-same as the previous code except that it will compile. You are creating a pointer and assigning a value to the pointer instead of assigning a memory address to it. The code is valid but has a run-time error. The pointer ip
is NOT pointing anywhere before you assigned value to it.
To make it work properly, you can do:
int val = 100;
int *ip = new int;
*ip = val;
Now, you have a pointer with a memory address. The value of that pointer is val
which is 100. That's the value NOT the memory address.
To conclude this, pointer must be initialized or it will have undefined behavior or even crash. You use the ref sign (&) or the new
keyword to initialize pointers then you can assign assign values to it.
int val = 100;
int *ip = new int; //Create a new pointer and initilize it with a new memory address
*ip = val; //Assign the value from val(100) to the pointer
OR
int val = 100;
int *ip = &val; //Create a new pointer and initialize it by making it point to existing address of **val**
*ip = 200; //change the value in the **memory addresss to **200**
Now val changes to 200 too.
int otherValue = 500;
*ip = otherValue ; //change the value in the **memory address** to 500
Now val changes to 500 too.
CHANGE MEMORY ADDRESS WHERE ip IS POINTING
You can also change the memory address anytime. Lets make p point to otherValue.
*ip = &otherValue; //Point p to otherValue memory address not value
*ip = 5; //Change the value of p to 5(no memory address change)
Now otherValue
has 5 as the value. p
points to otherValue
memory address.
Upvotes: 3
Reputation: 727017
Neither one of your two examples contains a pointer to reference:
int
, properly initialized using the address-of operator &
int
, improperly initialized to the value of val
itselfGenerally, you do not assign values to pointers directly: normally, you want an address-of operator, an implicit pointer conversion, or some pointer arithmetic. Although assigning a well-known address on specific hardware is also possible, compiler toolchains often provide ways to keep this outside of your program. Hence, if you see an assignment int *ip = 100
it's almost certainly incorrect.
If you want a reference, declare it like this:
int val = 100;
int &ref = val;
Now you can make a pointer to that reference, like this:
int *ipRef = &ref;
Since ref
represents an alias for val
, ipRef
would have the same value as ip
from your example (demo).
Upvotes: 3
Reputation: 27548
Because pointers and integers are different data types.
int *ip = &val;
This initialises one int*
with another int*
(the value obtained by taking the address of val
).
int *ip = val;
This is an attempt to initialise an int*
with an int
, which is not allowed.
Note that your code examples do not contain any references.
Upvotes: 0
Reputation: 48297
val is an integer
, so you can not assign an int to a pointer that points to an int because is not the valid value, you will need the address (obtained with the & operator)
Upvotes: 0