Home > Net >  Is this Pointer drawing correct?
Is this Pointer drawing correct?

Time:06-19

is this drawing correct? if it's not right, can you correct me?

char a[] = "halli";
const char *s = "hallo";
char *t = NULL;

pointer draw

CodePudding user response:

There is no "null" that t would be "pointing" to.

CodePudding user response:

a and s are correct (as drawings go).

t doesn’t "point to" NULL, t is NULL (stores a null pointer value).

The memory map would look something like this (addresses are for illustration only, assumes big-endian representation):

Address    Item    00  01  02  03
–––––––    ––––    —–  ––  ––  ––
0x1000        a    'h' 'a' 'l' 'l'
0x1004             'i' 00  ??  ??
0x1008        s    00  00  80  00
0x100c        t    00  00  00  00
...
0x8000             'h' 'a' 'l' 'l'
0x8001             'o' 00  ??  ??

a is an array of char and stores the contents of the string "halli" starting at address 0x1000. s is a pointer to char and stores the address of the first character of the string literal "hallo", which starts at 0x8000. t is a pointer to char and stores a null pointer value, which I’ve represented as 0x0000.

While the null pointer constant (NULL or the constant expression 0 in a pointer context) is always zero-valued, the null pointer value used by the execution environment doesn’t have to be - it only has to compare unequal to any pointer to an object or function.

  • Related