gpt4 book ai didi

c - 我如何将字符串添加到c中的字符串数组?

转载 作者:太空宇宙 更新时间:2023-11-04 03:25:42 25 4
gpt4 key购买 nike

我正在尝试向数组中添加一个字符串,但我不确定这段代码为何不起作用。有人可以给我一些反馈吗?

    /* Exercise b: Add <string> to the end of array <array>.
* Returns: pointer to the array after the string has been added.
*/
char **add_string(char **array, const char *string) {
/*reallocate so the array of strings can hold one more string*/
char** newArray = realloc(array, sizeof (array) + sizeof (char*));
if (!newArray) return array;

/*allocate memory for new string*/
char* newString = malloc(strlen(string) * sizeof (char));
if (!newString) return newArray;

/*copy old string to new string*/
int lastIndex = sizeof (newArray) / sizeof (char*);
strncpy(newString,string,strlen(string));

/*null terminate array and set old end of array to new string*/
newArray[lastIndex] = NULL;
newArray[lastIndex-1] = newString;

return newArray;
}

当我运行代码时,核心转储,但我不明白为什么

我问的原因是因为我知道传递数组的大小很容易。但是对于这种方法,我是不允许的。不幸的是,我的教授确实说过功能参数必须保持不变

最佳答案

有几处错误,例如sizeof (array) + sizeof (char*),其中sizeof(array)是错误的,+也是(应该是 *);以及上述所有其他评论也适用。

写了一个解决方案并留下你的代码作为评论;查看差异。

/* Exercise b: Add <string> to the end of array <array>.
* Returns: pointer to the array after the string has been added.
*/
char **add_string(char **array, const char *string) {

int lastIndex = 0;
while (array[lastIndex] != nullptr)
lastIndex++;

/*reallocate so the array of strings can hold one more string; note that lastIndex is zero-based; hence, the size of the current array is lastIndex+1, and consequently the size of the new array needs to be lastIndex+2 */
// char** newArray = (char**)realloc(array, sizeof (array) + sizeof (char*));
char** newArray = (char**)realloc(array, (lastIndex+2) * sizeof (char*));
if (!newArray) return array;

/*allocate memory for new string*/
char* newString = strdup(string);
//char* newString = malloc(strlen(string) * sizeof (char));
if (!newString) return newArray;

/*copy old string to new string*/
//int lastIndex = sizeof (newArray) / sizeof (char*);
//strncpy(newString,string,strlen(string));


/*null terminate array and set old end of array to new string*/
//newArray[lastIndex] = NULL;
//newArray[lastIndex-1] = newString;

newArray[lastIndex++] = newString;
newArray[lastIndex] = nullptr;

return newArray;
}

void printArray (char** array) {
char* str; int i=0;
while ((str = array[i]) != nullptr) {
std::cout << i << ":" << str << std::endl;
i++;
}
}

int main() {

char** myArray = (char**) malloc(1 * sizeof(char*));
myArray[0] = nullptr;

std::cout << "empty:" << std::endl;
printArray (myArray);

myArray = add_string(myArray, "Tom");
std::cout << "one element:" << std::endl;
printArray (myArray);

myArray = add_string(myArray, "Jerry");
myArray = add_string(myArray, "Fin");
std::cout << "three elements:" << std::endl;
printArray (myArray);

return 0;
}

关于c - 我如何将字符串添加到c中的字符串数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41252229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com