Reputation: 391
In last two statements, any real difference between the realPointer
and fakePointer
. Will they both provide the same functionality?
int num = 0;
int *realPointer = #
int fakePointer = #
Upvotes: 10
Views: 1591
Reputation: 213513
int fakePointer = #
This is not valid C, it breaks the rules of simple assignment and will not compile.
However, had you done int fakePointer = (int)#
then the only difference would be that there are no guarantees that you can use fakePointer reliably, conversions from pointers to integers are implementation-defined and could potentially also lead to undefined behavior (6.3.2.3):
Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
To safely and portably convert between pointers and integers, you should not use int
but the type uintptr_t
found in stdint.h.
Upvotes: 24
Reputation: 70901
Besides the fact that this
int fakePointer = #
may lead to one or more of the following
The following differences apply:
*
-/dereferencing-operator cannot by applied to the int
typed fakePointer
.fakePointer++
would most likely result in something different then doing realPointer++
. This applies to all other ways of adding and subtracting any value (but 0
). Read on "pointer arithmetic" vs. "arithmetic" for details.[]
-/indexing-operator to the int
typed fakePointer
.Upvotes: 1
Reputation: 96
If you go for the literal values (i.e. actual values) held by these two variables, they are same(i.e. address of variable num) but only values are same. But as told by others they are semantically two different variables and cannot be used interchangeably.
Coming to your last question:
Will they both provide the same functionality? Answer: No, they do not. Reasoning they are not of the same type. Your first question: any real difference between the realPointer and fakePointer. There are so many differences, and the most basic differences are:Upvotes: 1
Reputation: 17668
int num = 0;
int *realPointer = #
int fakePointer = #
In last two statements, any real difference between the realPointer and fakePointer.
Your fakePointer
is NOT a pointer. It's an integer with the value as the address of variable num. You may get away when compiling this with default options. But as Lundin pointed out, this is indeed an invalid code. Using gcc
with CFLAGS="-g -Wall -std=c99 -O3 -pedantic-errors"
flag, you'd get this error:
error: initialization makes integer from pointer without a cast
While your realPointer
really does point to variable num
, and you can dereference it. You can do nothing like that with your fakePointer
- and in fact the assignment to fakePointer
itself is invalid.
Upvotes: 4
Reputation: 409166
A variable declared as a pointer is semantically different from a variable not declared as a pointer. The compiler (and the language) will allow you do do things with a pointer you can not do with a non-pointer. And the opposite of course.
The biggest different is that you can dereference a pointer, but you can't do that to a non-pointer.
For example, using the variables in your code, then we can do *realPointer = 5
to make num
be assigned the value 5
, but doing *fakePointer = 5
is not allowed and doesn't make sense since fakePointer
isn't actually a pointer.
Upvotes: 6