[c] Print the address or pointer for value in C

I want to do something that seems fairly simple. I get results but the problem is, I have no way to know if the results are correct.

I'm working in C and I have two pointers; I want to print the contents of the pointer. I don't want to dereference the pointer to get the value pointed at, I just want the address that the pointer has stored.

I wrote the following code and what I need to know is if it is right and if not, how can I correct it.

/* item one is a parameter and it comes in as: const void* item1   */
const Emp* emp1 = (const Emp*) item1; 

printf("\n comp1-> emp1 = %p; item1 = %p \n", emp1, item1 );

While I'm posting this (and the reason it is important that it is correct) is that I eventually need to do this for a pointer-to-a-pointer. That is:

const Emp** emp1 = (const Emp**) item1; 

This question is related to c pointers memory-address

The answer is


What you have is correct. Of course, you'll see that emp1 and item1 have the same pointer value.


I have been in this position, especially with new hardware. I suggest you write a little hex dump routine of your own. You will be able to see the data, and the addresses they are at, shown all together. It's good practice and a confidence builder.


Since you already seem to have solved the basic pointer address display, here's how you would check the address of a double pointer:

char **a;
char *b;
char c = 'H';

b = &c;
a = &b;

You would be able to access the address of the double pointer a by doing:

printf("a points at this memory location: %p", a);
printf("which points at this other memory location: %p", *a);

I believe this would be most correct.

printf("%p", (void *)emp1);
printf("%p", (void *)*emp1);

printf() is a variadic function and must be passed arguments of the right types. The standard says %p takes void *.


char c = 'A';
printf("ptr: %p,\tvalue: %c,\tand also address: %zu", &c, c, &c);

Result:

ptr: 0x7ffc48e5105f,    value: A,    and also address: 140721531457631

To print address in pointer to pointer:

printf("%p",emp1)

to dereference once and print the second address:

printf("%p",*emp1)

You can always verify with debugger, if you are on linux use ddd and display memory, or just plain gdb, you will see the memory address so you can compare with the values in your pointers.


Examples related to c

conflicting types for 'outchar' Can't compile C program on a Mac after upgrade to Mojave Program to find largest and second largest number in array Prime numbers between 1 to 100 in C Programming Language In c, in bool, true == 1 and false == 0? How I can print to stderr in C? Visual Studio Code includePath "error: assignment to expression with array type error" when I assign a struct field (C) Compiling an application for use in highly radioactive environments How can you print multiple variables inside a string using printf?

Examples related to pointers

Method Call Chaining; returning a pointer vs a reference? lvalue required as left operand of assignment error when using C++ Error: stray '\240' in program Reference to non-static member function must be called How to convert const char* to char* in C? Why should I use a pointer rather than the object itself? Function stoi not declared C pointers and arrays: [Warning] assignment makes pointer from integer without a cast Constant pointer vs Pointer to constant How to get the real and total length of char * (char array)?

Examples related to memory-address

print memory address of Python variable Correct format specifier to print pointer or address? How to print variable addresses in C? C++ Double Address Operator? (&&) Difference between logical addresses, and physical addresses? Memory address of variables in Java Print the address or pointer for value in C Accessing Object Memory Address