[Solved] Why char pointer could add value after it signed? [closed]


TL;DR

*p equals c

Explanation

Hi, first take a look at how to format a question on stack overflow. You should write your code directly in the question.

So your question is about the understanding of these lines:

    char c,*p;
    p = &c
    *p = 'Z'
    printf("%c", c) // 'Z'

First you’re declaring two different type of variable:

  • char c; a char can store in memory 1 byte
  • char *p; a pointer of char. A pointer hold a memory address. In this case the memory address of a char

Then by doing p = &c you’re storing the memory address of c in the pointer p.

&c means the memory address of c.

*p means the value of the memory address stored in p

Finally by doing *p = 'Z' you’re setting the value at the memory address stored in p to ‘Z’. And because p store the memory address of c, c is set to ‘Z’.

If you still not confident about this, make some research about pointers.

3

solved Why char pointer could add value after it signed? [closed]