我有以下代码:更改内存地址*
str = "ABCD"; //0x001135F8
newStr = "EFGH"; //0x008F5740
*str
在第5位的realloc后 - //0x001135FC
我希望它指向:0x008F5740
void str_cat(char** str, char* newStr)
{
int i;
realloc(*str, strlen(*str) + strlen(newStr) + 1); //*str is now 9 length long
// I want to change the memory reference value of the 5th char in *str to point to newStr.
// Is this possible?
// &((*str) + strlen(*str)) = (char*)&newStr; //This is my problem (I think)
}
你究竟想达到什么目的?您可以将指针更改为指向任意内存位置,但不能将一个指针指向两个位置或不连续的内存块。 – DCoder
我不想指向2个地方。我想指向一个非连续的块(char数组中的第5个位置将指向内存中的不同位置)。这不可能? –
不,这是不可能的。 c中的字符串是char的CONTINUOS数组,它以0结尾 - 还有其他字符串实现,它本身表示一个字符串(通常是一个很长的字符串)作为内部字符串列表,但通常用于特殊用途 –