What is the difference between these declarations?
Which one of the following is correct and why is that method correct?
int val;
int *ptr = val; // first method
int *ptr_2 = &val; // second method
int *ptr_2 = &val;
is correct.
The first part ( int *ptr_2
) creates a pointer to an int
.
The second part ( &val
) returns the address of the variable val
.
int *ptr = val;
would assign the value of val
to ptr2
.
Technically, the first one may work (the compiler might shows a warning or a casting error, see the comments). If an address is stored as an integer in val
, that address could be used that way but it would make little to no sense to store an address in an int
.
Also note that val
will contain a random value(actually what was in that point of the RAM) if you don't initialize it.
why is the first method wrong?
I'm not sure what you want to do but the first method is probably not doing what you expect .
Therefore, the compiler will give you a warning message unless you write the first method like this:
int * ptr = (int *)val;
Doing so you tell the compiler that you know what you are doing and you want the compiler to behave as the "first method" behaves.
As user "pmg" wrote in his comment, the "first method" may be useful if you directly access hardware such as in microcontroller programming or when developing an operating system.
What is the difference of these declarations?
Let's make the program a bit more complicated to see what is happening:
int val;
int hello;
int world;
int *ptr1;
int *ptr2;
int *ptr3;
int *ptr4;
int *ptr5;
int *ptr6;
val = (int)&hello; // line "1"
ptr1 = &val; // second method
ptr2 = val; // first method (1)
val = (int)&world; // line "2"
ptr3 = &val; // second method
ptr4 = val; // first method (2)
val = 0;
ptr5 = &val; // second method
ptr6 = val; // first method (2)
The " =
" operator copies the value on the right side into the variable on the left side.
&val
means: "A pointer to the variable val
".
Therefore the pointer to the variable val
will be copied into the variables ptr1
, ptr3
and ptr5
and these three pointer variables will point to the variable val
.
The value of the variable val
does not matter in this case.
val
however means: "The value of the variable val
".
If we are using a 32-bit Windows or Linux operating system, we have written the address of the variable hello
to the variable val
in the line marked as "line 1".
The value of val
is copied to ptr2
in the line marked as "first method (1)", so ptr2
contains a pointer to hello
and not a pointer to val
.
And of course the value of val
does matter:
The variable ptr4
will contain a pointer to the variable world
and the variable ptr6
will have the value NULL
.
Using a 64-bit Windows or Linux operating system, ptr2
and ptr4
may even have invalid values in the example above.
What you try to do is valid in both cases but you do not use the feature correctly in the 1st case.
int val;
int *ptr_2 = &val; // second method
This is correct and it will work.
On the other hand,
int val;
int *ptr = val; // first method
does not work. What you try to do is something like that:
int val = some_integer;
int *ptr = val;
In this case, if some_integer
may be some valid port number; you will be able to access the port to write/read it, by using the ptr
variable. Most correct it would be you to declare ptr
as volatile
for such a purpose. However, this use of pointers is implementation defined and it's not portable from one compiler to the other or from one system to the other -- it is useful for embedded systems and using a particular compiler. The exact definition of casting int to pointer is so:
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.56)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.