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

后端 未结 8 702
醉话见心
醉话见心 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:27

    char p[3] = "hello" ? should be char p[6] = "hello" remember there is a '\0' char in the end of a "string" in C.

    anyway, array in C is just a pointer to the first object of an adjust objects in the memory. the only different s are in semantics. while you can change the value of a pointer to point to a different location in the memory an array, after created, will always point to the same location.
    also when using array the "new" and "delete" is automatically done for you.

提交回复
热议问题