c中的链表,结构的结构,分段错误

时间:2016-11-07 16:42:10

标签: c struct linked-list segmentation-fault

我试图在c中实施列车结构。我有一个名为bogie的结构,另一个叫做链接转向架。我在如何在代码中引用这些结构的所有组件时遇到问题。 我的主要问题是:

- 我在进入转向架类型时遇到了段错误

- 为什么转向架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,capacity\n");
    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 train\n");
    printf("----------------------------------------\n");


    do
    {
        printf("Bogie number:%d\n",count);
        printf("Bogie name:%c\n",traverse->bogie_part_address->name);
        printf("Bogie type:%s\n",traverse->bogie_part_address->type);
        printf("Bogie capacity:%d\n",traverse->bogie_part_address->capacity);
        printf("----------------------------------------\n");
    }
    while(traverse->link!=NULL);

}

int main()
{

    printf("linked bogie size:%lu\n",sizeof(LINKED_BOGIE));
    printf("bogie size:%lu\n",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 train\n\
2)Add a bogie\n\
3)Insert a bogie\n\
4)Remove a bogie\n\
5)Sort train\n\
6)Swap bogies\n\
7)Exit\n");

        scanf("%d",&choice);

        switch (choice)
        {
            case 1:print_train(engine_address);break;
            case 2:add_bogie(engine_address);
        }

    }

    while(choice!=7);




return 0;

}

1 个答案:

答案 0 :(得分:1)

迈克已经解释过,结构的大小是由填充引起的。在您的系统中,int使用4个字节,并在4的地址倍数上对齐。

你的mallocs是正确的,但是在以后移除一个boogie时你必须始终释放内部和外部结构。

但您的代码中存在小错误:

  • scanf("%c%s%d", ...)执行您的预期:%c可能会读取上一行的结尾而不是第一个非空白字符。您应该使用%1s,而不会忘记null的位置:

    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);也不起作用:当列车只包含一个布吉时,在测试任何东西之前将遍历设置为null。你应该这样做:

    while(traverse->link!=NULL) {
        traverse = traverse->link;
    }
    
  • 在`print_traint中,你忘了移动遍历。它应该是:

    while(traverse != NULL)
    {
        ....
        traverse = traverse->link;
        count += 1;
    }
    

记住:

  • %c很少使用,因为它甚至可以读取其他格式忽略的不可打印字符
  • do {...} while(...);如果使用的次数少于简单的while(...) {...},因为它至少会运行一次。