Why must a pointer to a char array need strcpy to assign characters to its array and double quotes assignment will not work?

前端 未结 9 1758
耶瑟儿~
耶瑟儿~ 2021-02-14 09:23

The first example does not work when you go to delete the pointer. The program either hangs when I add the null terminator or without it I get:

Debug Assertion Fai

9条回答
  •  离开以前
    2021-02-14 10:07

    A pointer holds an address. The = operator for a pointer changes the address held.

    at = "tw";
    

    Makes at point to the array "tw" (an array created by the compiler to hold the characters tw), it no longer points to the array you created with new. created in the file.

    at[2] = '\0';
    

    Adds a NULL to the end of the complier array.

提交回复
热议问题