繁体   English   中英

strcat function 不能使用指针

[英]strcat function not working with pointers

int main(int argc , int *argv[]){

char str[7] = "Mobile";
char str2[11] = "Samsung4g";

char *str3 = 0;
str3 = ( char* )(malloc(30));

str3 = "Carrier";

strcat(str3, str);//Problem facing here, dunno why

就像其他人在评论中提到的那样,声明:

str3 = "Carrier";

用字符串文字“Carrier”的地址覆盖 str3 的值,导致 malloc() 返回的地址丢失。

然后在 strcat() 中使用 str3 作为第一个参数尝试修改导致未定义行为的字符串文字。

使用 strcpy() 或等效函数复制 C 中的字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM