Printing an address with a pointer contains in C

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

I work in C and I have two pointers; I want to print the contents of a pointer. I don’t want to play the pointer to get the specified value, I just want the pointer to be stored.

I wrote the following code, and I need to know if this is correct, and if not, how can I fix 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 am posting this (and the reason why it is important that it is correct), I should end up doing this for a pointer to a pointer. I.e:

 const Emp** emp1 = (const Emp**) item1; 
+68
c pointers memory-address
Jun 28 '09 at 22:48
source share
6 answers

What you have is right. Of course, you will see that emp1 and item1 have the same pointer value.

+23
Jun 28 '09 at 22:53
source share

To type an address in a pointer to a pointer:

 printf("%p",emp1) 

dereference once and print the second address:

 printf("%p",*emp1) 

You can always check with the debugger if you use Linux ddd and display memory, or just just gdb , you will see the memory address so that you can compare with the values ​​in your pointers.

+33
Jun 28 '09 at 22:53
source share

I think that would be the most correct.

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

printf() is a variational function and arguments of the correct types must be passed. The standard states that %p occupies void * .

+13
Jun 29 '09 at 1:19
source share

Since you already seem to decide to display the main pointer address, here is how you would check the address of the double pointer:

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

You can access the double pointer address a by doing:

 printf("a points at this memory location: %p", a); printf("which points at this other memory location: %p", *a); 
+8
Jun 28 '09 at 23:01
source share
 char c = 'A'; printf("ptr: %p,\t value: %c,\t and also address: %zu",&c, c,&c); 

result:

ptr: 0xbfb4027f, value: A, and also address: 3216245375

+3
Mar 11 '13 at 11:48
source share

I was in this position, especially with new equipment. I suggest you write a small manual dump. You can see the data and the addresses where they are located, shown together. This is a good practice and trust builder.

0
Jun 29 '09 at 3:18
source share



All Articles