我正在尝试通过更改指针来更改原始字符串的值。使用指针更改原始字符串的值
说我有:
char **stringO = (char**) malloc (sizeof(char*));
*stringO = (char*) malloc (17);
char stringOne[17] = "a" ;
char stringTwo[17] = "b";
char stringThree[17] = "c";
char newStr[17] = "d";
strcpy(*stringO, stringOne);
strcpy(*stringO, stringTwo);
strcpy(*stringO, stringThree);
//change stringOne to newStr using stringO??
如何使用指针stringO
我改变stringOne
所以它的同newStr
?
编辑:我想这个问题还不是很清楚。我希望它修改*strcpy
被复制的最新字符串。所以,如果strcpy(*stringO, stringThree);
最后调用,它会修改stringThree
,strcpy(*stringO, stringTwo);
然后string Two
等
您不应该投射'malloc'的结果。 – chris
@chris([解释](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858)) – 2013-08-16 05:14:25
@ H2CO3该线程上的其他答案指向包含演员的充分理由 – gta0004