Reputation: 750
Normal pointer usage as I have been reading in a book is the following:
int *pointer;
int number = 5;
pointer = &number;
Then *pointer has a value of 5. But does this work the other way around? I mean:
int *pointer;
int number = 5;
*pointer = &number;
Here does pointer contain the value 5 and *pointer holds the address of number?
Thanks.
Upvotes: 0
Views: 2693
Reputation: 67723
For you to write an assignment x = y
, both x
and y
must be, or be implicitly convertible to, the same type (or x
must have a user-defined assignment operator taking an argument matching the type of y
, or ... OK, there are a few possibilities, but you get the idea).
Now, let's look at the statement
*pointer = &number;
Here, *pointer
has type int&
- you followed the pointer to get a (reference to) the integer stored at that location. Let's ignore, for now, the fact that your pointer was uninitialized and following it results in undefined behaviour.
The right hand side, &number
, is taking a pointer to an integer variable, so the type is int*
.
So, the expression doesn't make sense at all, just in terms of the type system: there is no way to assign int*
to int&
. It doesn't mean anything.
Let's relate it back to the English language of your question
Here does pointer contain the value 5 and *pointer holds the address of number?
That translates directly to the type system, and hence also doesn't mean anything. Again, we can break it down
does pointer contain the value 5
a pointer contains a location. The only time it would make sense to talk about a pointer having a literal value (other than nullptr
) would be on a platform where there were well-known addresses - this is rare, and '5' is very unlikely to be a well-formed address anyway
*pointer holds the address
well, there is a case where *pointer
could hold an address - it's where *pointer
is itself is a pointer, meaning the variable pointer
is a pointer-to-pointer such as int **
. That isn't the case here: we know the type of *pointer
in your code, and it is int&
, not int*
.
Upvotes: 1
Reputation: 69
When you create a pointer variable, initially it will have garbage value (let say 300 address location). Hence when you dereference pointer(*300), it would give another garbage value(value at 300 address location) or error (strictly speaking anything may happen depending on your computer).
In the third step, &number:- which is also another number and your are trying to assign a number to *pointer(may be another number) which not possible. (It is like this:- 5=6). Hence it will be an error.
Upvotes: 1
Reputation: 66371
Analogy from the department of far-fetched analogies:
You use pointers many times every day without even thinking about it.
A pointer is an indirection - it tells you where something is, or how it can be reached, but not what that thing is.
(In a typed language, you can know what kind of thing it is, but that's another matter.)
Consider, for example, telephone numbers.
These tell you how to reach the person that the phone number belongs to.
Over time, that person may change - perhaps somebody didn't pay their bills and the number was reassigned - but as long as the number is valid, you can use it to reach a person.
Using this analogy, we can define the following operations:
&person
gives you a person's phone number, and*number
gives you the person that the number belongs to.Some rules:
&number
is an error, as is *person
.Now, your first example would translate to
PhoneNumber n;
Person Bob;
n = &Bob;
which makes sense; n
now holds Bob's phone number.
Your second example would translate to
PhoneNumber n;
Person Bob;
*n = &Bob;
which would say "replace the General Manager of Acme Inc's Customer Services with Bob's phone number", which makes no sense at all.
And your final question,
Here does pointer contain the value 5 and *pointer holds the address of number?
would translate to
Is this phone number the same thing as Bob, and if you call it, will Bob's phone number answer?
which I am sure you can see is a rather strange question.
Upvotes: 2
Reputation: 213523
*pointer = &number;
is not valid C.
*pointer
is of type int
and &number
is of type int*
. This isn't a valid form of assignment and will not compile on a standard C compiler.
You can store numbers inside pointer variables, but then you must use an explicit cast to force a type conversion. Some compilers allow it without an explicit cast, but note that doing so is a non-standard extension.
And of course, note that you haven't set pointer
to point at an allocated memory address, so you can't store anything inside where it points.
If you do an explicit cast such as
pointer = &something;
*pointer = (int)&number;
then it is allowed in C, but if you try to de-reference that pointer, the behavior is implementation-defined. It could possibly also be undefined behavior in case of misalignment etc. See C11 6.3.2.3:
An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.
Upvotes: 2
Reputation: 36597
Your second case will not compile, because the assignment *pointer = &number
involves incompatible types (int
on the left, a pointer to int
on the right) which makes the assignment invalid.
If you somehow coerce the assignment into compiling, then pointer
is not initialised. Accessing its value, let alone dereferencing it (e.g. evaluating *pointer
or assigning to it as in *pointer = number
or *pointer = 5
) gives undefined behaviour. Anything can happen then .... depending on circumstances, a common result of undefined behaviour is an abnormal program termination.
Upvotes: 2
Reputation: 94299
In
int *pointer;
int number = 5;
*pointer = &number;
you never assign a valid address to pointer
, so dereferencing that pointer (as is done by the expression *pointer
) is not valid.
Upvotes: 3