assigning a string to another string

后端 未结 5 1007
没有蜡笔的小新
没有蜡笔的小新 2021-01-28 09:11

Why this code is not running? Why str1 is not assigned to str2 ?? I know i have an option of using strcpy but i wish to know the reason why this is not working??



        
5条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-28 09:13

    str2 is a static array. You can't just reassign it like a pointer.

    As for your pointer example, q=s reassigns q to point to the same space that s is pointing to. However, the pointer reassignment does not copy the value.

    For static arrays, use strcpy() to copy the value. For pointers, use strdup().

提交回复
热议问题