我有以下代碼:更改內存地址*
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結尾 - 還有其他字符串實現,它本身表示一個字符串(通常是一個很長的字符串)作爲內部字符串列表,但通常用於特殊用途 –