-2

I have this bit of code:

printf("Address for ptr_one %p\n", &ptr_one);
printf("Non-Address for ptr_one %p\n", ptr_one);
printf("value for ptr_one %p\n\n", *ptr_one);

output:

Address for ptr_one 0xffffcbd8
Non-Address for ptr_one 0x600042b26
value for ptr_one 0x19

From what I understand the first line is the pointer address, and the third is the value which is at that address. But what is the second line printing out exactly?

It was initialized as such:

int *ptr_one = (int *)malloc(sizeof(int));
*ptr_one = 25;

edit: added initialize code

  • 5
    Depends on the definition of `ptr_one` and how it was assigned. – dbush Oct 24 '16 at 01:20
  • 3
    Nah, the first one is printing the address of the variable that holds a pointer to something else. The second prints that pointer. - Assuming of course, the name is actually representative of the content and it's been initiated correctly, like dbush said. – enhzflep Oct 24 '16 at 01:21
  • `printf("Non-Address for ptr_one %p\n", ptr_one);` this is the address that holds `*ptr_one`. `printf("Address for ptr_one %p\n", &ptr_one);` this is the address of `ptr_one` – tesseract Oct 24 '16 at 01:21
  • The first prints the address of the variable ptr_one, the 2nd prints the values that is stored in the ptr_one as a pointer address value. – Mox Oct 24 '16 at 01:27
  • [Don't cast the result of `malloc` in C](http://stackoverflow.com/q/605845/995714) – phuclv Oct 24 '16 at 01:38
  • yeah sorry that i didn't put in how I initialized ptr_one I though it wasn't important but it seem like it is – Michael Tran Oct 24 '16 at 01:40

4 Answers4

2

It looks like you have code along the lines of:

int one = 0x19;
int *ptr_one = &one;

(you've since edited your code to show the actual situation but it's functionally identical to what I've shown above (in terms of memory and pointers to memory anyway) so that I don't need to change it).

What you then have is:

(ptr_one "variable")
    |
    V
+---------------+     +-----------+
| ptr_one value | --> | one value |
+---------------+     +-----------+

with the following descriptions:

  • Address for ptr_one 0xffffcbd8 is the address of the actual ptr_one variable, where it's stored in memory.
  • Non-Address for ptr_one 0x600042b26 is the value of the ptr_one variable which is also the address of the one variable.
  • value for ptr_one 0x19 is the value of the one variable.
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

The first line is the pointer's memory address, the second line is the address that the pointer points to, and the third is the first value which is at that address.

IMXQD
  • 231
  • 2
  • 11
0

Your example is a bit terse and doesn't mention the type of ptr_one. Suppose you have

int x=10;
int *ptr_one=&x;

Then

printf("Address for ptr_one %p\n", &ptr_one);

prints the address of the pointer ie ptr_one

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

prints the address of x (ptr_one is a pointer after all.)

printf("ptr_one points to %p\n\n", *ptr_one);

prints the value of x (Here you dereference the pointer to gets the value stored in it)

sjsam
  • 21,411
  • 5
  • 55
  • 102
0

The ptr_one is an intiger pointer present in address location 0xffffcbd8. Currently it points to an address 0x600042b26. In address 0x600042b26 we have the value 25 in it.

This link shows how ptr_one is allocated in memory

Hope this provides the answer..

Alex
  • 1
  • 2