gpt4 book ai didi

c - 如何在 C 中为我的数据结构分配内存

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

我不明白为什么我的代码在用户输入他们的数据后打印出垃圾变量。在我的intro to C class中学习数据结构,链表和内存分配的过程中。感谢您的帮助!

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

typedef struct AutoMobilesInfo
{
char *Manufacturer, *ModelNameOfCar, *ColorOfCar;
int YearOfCar;
struct AutoMobilesInfo *next;
} Car;

void EnterCarInfo(Car *Info)
{
Info = (Car *)malloc(sizeof(Car));
Info->Manufacturer = (char *)malloc(12 *(sizeof(char)));
Info->ModelNameOfCar = (char *)malloc(12 *(sizeof(char)));
Info->ColorOfCar = (char *)malloc(12 *(sizeof(char)));

printf("Please enter the car's manufacturer: \n");
scanf(" %s", Info->Manufacturer);
printf("Please enter the car's model: \n");
scanf(" %s", Info->ModelNameOfCar);
printf("Please enter the car's color: \n");
scanf(" %s", Info->ColorOfCar);
printf("Please enter the year the car was made: \n");
scanf("%d", &Info->YearOfCar);
}

void PrintedOutCarInfo(Car *host)
{
host = (Car *)malloc(sizeof(Car));
host->Manufacturer = (char *)malloc(12 *(sizeof(char)));
host->ModelNameOfCar = (char *)malloc(12 *(sizeof(char)));
host->ColorOfCar = (char *)malloc(12 *(sizeof(char)));

printf("The car's manufacturer is %s\n", host->Manufacturer);
printf("The car's model is %s\n", host->ModelNameOfCar);
printf("The color of the car is %s\n", host->ColorOfCar);
printf("The year the car was made is %d\n", host->YearOfCar);
}

int main()
{
Car *Car1;
Car1 = (Car *) malloc(sizeof(Car));
EnterCarInfo(Car1);
PrintedOutCarInfo(Car1);
free(Car1);

return 0;
}

最佳答案

您正在覆盖 PrintedOutCarInfo 中的指针。这条线

host = (Car *)malloc(sizeof(Car));

还有这一行

Info = (Car *)malloc(sizeof(Car));

你传入的变量hostInfo被分配了。把那个函数改成这个

void PrintedOutCarInfo(Car *host) {
printf("The car's manufacturer is %s\n", host->Manufacturer);
printf("The car's model is %s\n", host->ModelNameOfCar);
printf("The color of the car is %s\n", host->ColorOfCar);
printf("The year the car was made is %d\n", host->YearOfCar);
}

您也不应该对 malloc 的返回进行 case,即改为这样做...

 Info = malloc(sizeof(Car));

试试这个...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

typedef struct AutoMobilesInfo
{
char *Manufacturer, *ModelNameOfCar, *ColorOfCar;
int YearOfCar;
struct AutoMobilesInfo *next;
} Car;

void EnterCarInfo(Car *Info)
{
Info->Manufacturer = malloc(12 * (sizeof(char)));
Info->ModelNameOfCar = malloc(12 * (sizeof(char)));
Info->ColorOfCar = malloc(12 * (sizeof(char)));

printf("Please enter the car's manufacturer: \n");
scanf(" %s", Info->Manufacturer);
printf("Please enter the car's model: \n");
scanf(" %s", Info->ModelNameOfCar);
printf("Please enter the car's color: \n");
scanf(" %s", Info->ColorOfCar);
printf("Please enter the year the car was made: \n");
scanf("%d", &Info->YearOfCar);
}

void PrintedOutCarInfo(Car *host) {
printf("The car's manufacturer is %s\n", host->Manufacturer);
printf("The car's model is %s\n", host->ModelNameOfCar);
printf("The color of the car is %s\n", host->ColorOfCar);
printf("The year the car was made is %d\n", host->YearOfCar);
}

int main()
{
Car *Car1 = malloc(sizeof(Car));
EnterCarInfo(Car1);
PrintedOutCarInfo(Car1);
free(Car1);
return 0;
}

关于c - 如何在 C 中为我的数据结构分配内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36414344/

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