What is the difference between char array and char pointer in C?

后端 未结 8 558
醉话见心
醉话见心 2020-11-22 06:03

I am trying to understand pointers in C but I am currently confused with the following:

  • char *p = \"hello\"
    

    This is a char pointer

8条回答
  •  灰色年华
    2020-11-22 06:16

    For cases like this, the effect is the same: You end up passing the address of the first character in a string of characters.

    The declarations are obviously not the same though.

    The following sets aside memory for a string and also a character pointer, and then initializes the pointer to point to the first character in the string.

    char *p = "hello";
    

    While the following sets aside memory just for the string. So it can actually use less memory.

    char p[10] = "hello";
    

提交回复
热议问题