I'm trying to implement a train structure in c. I have a struct called bogie and another called linked bogie. I'm having problems with how to refer to all components of these structures in the code. My main problems are:
-I get a seg fault while entering the bogie type
-Why is size of bogie 28? (char(1) +array(20) +int(4)=25)
-I want to confirm if there is a struct inside a struct,I need to malloc for every inner struct when i make an instance of the outer struct? Anyway to make this automatic.
-Am I over complicating the code? Is there a cleaner way to write this?
#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;
}
charwill occupy 4 bytes even though only one is used. If you make the string 21 characters the size will probably jump up to 32. This is because the CPU you are compiling for has a 32 bit data bus, so the fastest access is when data items are aligned on (32 bit) word boundaries. There may be a 'pack' pragma or attribute language extension on your compiler, which can remove the unused bytes, but it will slow all accesses to that structure down.