我试图在c中实现一个列车结构。我有一个结构称为转向架和另一个称为链接转向架。我有如何在代码中引用这些结构的所有组件的问题。我的主要问题是:
-输入转向架类型时出现segfault
-为什么转向架的尺寸是28?(char(1) +array(20) +int(4)=25)
-我想确认是否有一个结构内部的结构,我需要malloc为每一个内部结构当我做一个外部结构的实例?不管怎样,为了使它自动化。
-我是否使代码过于复杂?有没有更简洁的写法?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char name;
char type[20];
int capacity;
}BOGIE;
struct LINKED_BOGIE
{
BOGIE* bogie_part_address;
struct LINKED_BOGIE* link;
};
typedef struct LINKED_BOGIE LINKED_BOGIE;
void print_train(LINKED_BOGIE* engine_address);
void add_bogie(LINKED_BOGIE* engine_address);
void add_bogie(LINKED_BOGIE* engine_address)
{
LINKED_BOGIE* traverse=engine_address;
LINKED_BOGIE* new_bogie_address=(LINKED_BOGIE*)malloc(sizeof(LINKED_BOGIE));
new_bogie_ad:dress->bogie_part_address=(BOGIE*) malloc(sizeof(BOGIE));
printf("Enter bogie name,type,capacityn");
scanf("%c%s%d",&(new_bogie_address->bogie_part_address->name),(new_bogie_address->bogie_part_address)->type,&((new_bogie_address->bogie_part_address)->capacity));
do traverse=traverse->link;
while(traverse->link!=NULL);
traverse->link=new_bogie_address;
new_bogie_address->link=NULL;
print_train(engine_address);
}
void print_train(LINKED_BOGIE* engine_address)
{
LINKED_BOGIE* traverse=engine_address;
int count=0;
printf("This is the trainn");
printf("----------------------------------------n");
do
{
printf("Bogie number:%dn",count);
printf("Bogie name:%cn",traverse->bogie_part_address->name);
printf("Bogie type:%sn",traverse->bogie_part_address->type);
printf("Bogie capacity:%dn",traverse->bogie_part_address->capacity);
printf("----------------------------------------n");
}
while(traverse->link!=NULL);
}
int main()
{
printf("linked bogie size:%lun",sizeof(LINKED_BOGIE));
printf("bogie size:%lun",sizeof(BOGIE));
LINKED_BOGIE* engine_address=(LINKED_BOGIE*)malloc(sizeof(LINKED_BOGIE));
engine_address->bogie_part_address=(BOGIE*)malloc(sizeof(BOGIE));
engine_address->bogie_part_address->name='E';
strcpy(engine_address->bogie_part_address->type,"Engine");
engine_address->bogie_part_address->capacity=1;
engine_address->link=NULL;
// print_train(engine_address);
int choice=0;
do
{
printf("Pick what you want to do:n
1)View the trainn
2)Add a bogien
3)Insert a bogien
4)Remove a bogien
5)Sort trainn
6)Swap bogiesn
7)Exitn");
scanf("%d",&choice);
switch (choice)
{
case 1:print_train(engine_address);break;
case 2:add_bogie(engine_address);
}
}
while(choice!=7);
return 0;
}
Mike已经解释过结构体的大小是由填充引起的。在您的系统中,int类型使用4个字节,并按4的倍数地址对齐。
你的malloc是正确的,但是在以后移除boogie时,你必须始终释放内部和外部结构体。
但是你的代码中有一些小错误:
-
scanf("%c%s%d", ...)
将而不是执行您期望的操作:%c可能会读取前一行的末尾,而不是第一个非空白字符。您应该使用%1s
,而不要忘记空的位置:char name[2]; ... scanf("%1s%19s%d",name,(new_bogie_address->bogie_part_address)->type, &((new_bogie_address->bogie_part_address)->capacity)); new_bogie_address->bogie_part_address->name = name[0];
-
do traverse=traverse->link; while(traverse->link!=NULL);
也将不起作用:当列车只包含一个布吉时,在测试任何东西之前将导线设置为空。你应该这样做:while(traverse->link!=NULL) { traverse = traverse->link; }
-
在print_train中,您忘记移动遍历。应该是:
while(traverse != NULL) { .... traverse = traverse->link; count += 1; }
记住:
-
%c
很少使用,因为它读取甚至不可打印的字符,这些字符被其他格式忽略 -
do {...} while(...);
如果比简单的while(...) {...}
少使用,因为它至少运行一次它的主体。