0

I just got started learning about pointers and was executing some code:

#include <stdio.h>

int main(void){

    int num = 12;  // initialize num to 12
    int *pnum = NULL;  // initialize the pointer

    pnum = &num;  // stores the address of num in pnum

    printf("the address of the number is %p\n", &num);  // display the address of the number
    printf("the address of the pointer is %p\n", &pnum);  // display the address of the pointer
    printf("the value of the pointer is %p\n", pnum);  // display the value of the pointer
    printf("the value the pointer is pointing to is %d\n", *pnum);  // display the value the pointer is pointing to

    return 0;
}

In this code above, It prints out 0xffffcbec for the address of the number and value of the pointer, and 0xffffcbe0 for the address of the pointer. I want to know the reason. I feel like this is related to some incorrect inputs of data types.

I use VScode btw.

Joel Bodenmann
  • 2,152
  • 2
  • 17
  • 44
  • 3
    `I want to know the reason.` The reason for what? Its quite unclear what you're asking. – tkausl Aug 03 '22 at 15:11
  • The reason it prints 0xffffcbe0 for the address of `pnum` is because the address of `pnum` is 0xffffcbe0. – William Pursell Aug 03 '22 at 15:22
  • pointer is a var himself and so on have a dedicated address where it store the pointed address. so, 0xffffcbe0 hold your pointer data and his content store 0xffffcbec. it seem that the linker has stored pointer var before your int in your program memory, all is nice. – r043v Aug 03 '22 at 15:23
  • See [Does stack grow upward or downward?](https://stackoverflow.com/questions/1677415/does-stack-grow-upward-or-downward) – jarmod Aug 03 '22 at 15:25

1 Answers1

4

The value of pnum is the value of &num, i.e. the location of the variable num. Therefore &num and pnum will print the same.

To make it easier to understand and visualize, I recommend you draw it all out:

+-------+      +------+      +-----+
| &pnum | ---> | pnum | ---> | num |
+-------+      +------+      +-----+

That is:

  • &pnum is pointing to pnum
  • pnum (which is the same as &num) is pointing to num

Also, the pointer-to operator & and the dereference operator * are each others opposites.

So pnum is the value of &num. And *pnum is the value of num.

And *&num is plain num, as the operators cancel out each other.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I am sorry everyone who confused on my question. I was gonna clarify my question but stackoverflow was crashing at the time. SO, I was wondering, is there nothing wrong with my code? Because I thought it should print 8-bits but 0xffffcbec looks like more than 8-bits( 8-bits means basically 8 characters right?) and I assumed that there was an error. I want to know if the code is how it is supposed to happen. – Trevor Song Aug 04 '22 at 04:42
  • @TrevorSong A *bit* is a single `0` or `1`. A *byte* is a collection of (usually) 8 bits. An address on a 32-bit system is 4 bytes, and 8 bytes on a modern 64-bit system. A single hexadecimal digit represents 4 bits. – Some programmer dude Aug 04 '22 at 06:08