Home > Mobile >  confusing questions about pointers
confusing questions about pointers

Time:10-30

What is the difference between line 8 and line 9? if line 8 is showing the address of x, what is the address in line 9? What is the value of tx if it's not the address of x?

enter image description here

I tried to watch some videos about pointers but I still don't get them 100%

CodePudding user response:

The line

int tx = &x;

will not work on most modern 64-bit platforms, because an int usually has a size of 32 bits, whereas a pointer usually has a size of 64 bits. Therefore, on those platforms, an int is not large enough to store an entire pointer. Attempting to do this will cause the value to get truncated. This explains why you are seeing 0x7e4f7aa8 instead of 0x7ffc7e4f7aa8. The 7ffc is getting stripped in the truncation.

If you want to be sure that the integer type is large enough to store a pointer, you should use uintptr_t instead of int, like this:

uintptr_t tx = &x;

Also, ISO C requires a cast:

uintptr_t tx = (uintptr_t)&x;

The line

printf("%p\n", x);

will invoke undefined behavior, because the %p specifier requires an argument of type void *, but you are instead providing an argument of type int. If you want to print the value of the integer in hexadecimal, you should use the %x specifier instead of %p.

For the same reason, the line

printf("%p\n", tx);

will also invoke undefined behavior.

After applying all of the fixes mentioned above, your program should look like this:

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

int main( void )
{
    int x;
    int *px = &x;
    uintptr_t tx = (uintptr_t)&x;

    printf( "%p\n", (void*)&x );
    printf( "%x\n", (unsigned int)x );
    printf( "%p\n", (void*)px );
    printf( "%p\n", (void*)&px );
    printf( "%"PRIxPTR"\n", tx );
}

On the platform I am using, the macro PRIxPTR simply expands to "lx". However, using %lx for uintptr_t may not work on all platforms. Using this macro is therefore safer if you want the code to run on different platforms. This macro exists because there is no fixed printf specifier for the data type uintptr_t. See the documentation for uintptr_t for further information.

On the 64-bit Linux system I tested it on, this program has the following output:

0x7ffc6e95d7b4
7ffc
0x7ffc6e95d7b4
0x7ffc6e95d7a8
7ffc6e95d7b4

The second line of output is not important, because the variable x is not initialized, so that it happens to have the value of whatever value that memory location had beforehand.

CodePudding user response:

&x is the address of an integer and x is said integer. Line 8 prints the address out of the integer out, and line 9 is printing the integer value (which is undefined) as if it was a pointer. You should use %d instead of %p on line 9.

CodePudding user response:

It looks pretty basic, the line 8 printf("%p\n", &x) is printing the memory address of the variable x. (Location where it is stored). Line 9 is printing the VALUE of the variable x, but as it was not started and returned no value. (A "polluted" value that was at the address of this variable being returned by %p the correct one would be %d in the line 9)

CodePudding user response:

%p is used for specify address, but you can use it as hexadecimal converter for example:

#include <stdio.h>

int main(){
int a=10;
int *px=&a;// store the address of "a"

printf("%p\n",&a);// print the address of "a
printf("%p\n",a);//print the value of "a" in hexadecimal.
printf("%p\n",px);//print the address of "a" in hexa decimal
printf("%d\n",px);//print the address of "a" in decimal
return 0;

}

thanks

  • Related