gpt4 book ai didi

c - 打印双指针字符数组时出现运行时错误

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

我正在努力创建一个系统,用于将学生姓名和分数输入数组并将相同的信息打印到屏幕上,不幸的是,我一直收到奇怪的输出。

我使用调试器单步执行我的程序,它显示一切运行顺利,直到我到达打印学生信息的函数,双指针 char 数组已经弄乱了值。

这是我运行程序时看到的图像。 ( http://s28.postimg.org/nv29feawt/Error.png )

注意:虽然我知道有更好、更简单的方法可以做到这一点,但我需要使用动态分配的内存和数组来完成这项任务。

int main(void)
{
char **firstNames;
char **lastNames;
float *scores;

int recordsLength;
printf("Please indicate the number of records you want to enter: ");
scanf("%d", &recordsLength);
printf("\n\n");

firstNames = (char **)malloc(recordsLength * sizeof(char *));
lastNames = (char **)malloc(recordsLength * sizeof(char *));
scores = (float *)malloc(recordsLength * sizeof(float));

int i = 0;
while(i < recordsLength)
{
createNewEntry(i, firstNames, lastNames, scores);
i++;
}

printEntry(0, firstNames, lastNames, scores);
free(firstNames);
free(lastNames);
free(scores);
return 0;
}

void clearScreen()
{
#ifdef _WIN32
system("cls");
#elif _unix_
system("clear");
#endif
}

void printEntry(int entryID, char *firstNames[], char *lastNames[], float scores[])
{
clearScreen();
printf("|-------------------------------------------------------------------------|\n");
printf("| Student Entry |\n");
printf("|-------------------------------------------------------------------------|\n|\n");
printf("| First Name: %s Last Name: %s Score: %.1f\n|\n|\n|\n", firstNames[entryID], lastNames[entryID], scores[entryID]);
printf("|-------------------------------------------------------------------------|\n");
printf("| |\n");
printf("|-------------------------------------------------------------------------|\n\n");
}

void createNewEntry(int index, char *firstNames[], char *lastNames[], float scores[])
{
printf("Please input the records of the new student.\n\n\n");
char first[20];
char last[20];
float score = 100.0f;

printf("Please enter the student's first name: ");
scanf("%s", &first);
printf("\n\n");

printf("Please enter the student's last name: ");
scanf("%s", &last);
printf("\n\n");

printf("Please enter the student's score: ");
scanf("%f", &score);
printf("\n\n");

firstNames[index] = (char *)malloc((strlen(first)) * sizeof(char));
firstNames[index] = first;

lastNames[index] = (char *)malloc((strlen(last)) * sizeof(char));
lastNames[index] = last;
printf("first name: %s", firstNames[index]);
printf("last name: %s", lastNames[index]);
scores[index] = score;
}

最佳答案

firstNames[index] = (char *)malloc((strlen(first)) * sizeof(char));
firstNames[index] = first; /* You are missing your allocated memory block and assigning local */

上面几行是不正确的。您不能使用分配 = 运算符分配 c-strings。你应该为此使用 strcpy

您正在将本地数组分配给 firstnames,该数组在函数结束后没有生命。这会调用一个未定义的行为。 (您看到的是奇怪的字符,但情况可能更糟)。

应重写为(姓氏也类似)

firstNames[index] = malloc((strlen(first) + 1) * sizeof(char)); /* +1 for \0 */
if(firstNames[index] == NULL) {
/* Malloc failed, error handling */
...
}
/* else */
strcpy(firstNames[index], first); /* Use strcpy to copy contents */

Live example here

在释放 firstNameslastNames 之前,您应该循环释放 firstNames 和 lastNames 的所有成员。

关于c - 打印双指针字符数组时出现运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26923826/

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