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

后端 未结 8 543
醉话见心
醉话见心 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.

    0 讨论(0)
  • 2020-11-22 06:30

    You're not allowed to change the contents of a string constant, which is what the first p points to. The second p is an array initialized with a string constant, and you can change its contents.

    0 讨论(0)
提交回复
热议问题