c-从struct填充数组时出现分段错误

时间:2018-03-12 23:41:52

标签: c arrays struct segmentation-fault typedef

这段代码是我试图为座位安排做不同的功能:

#include <stdio.h>
#include <stdlib.h>
#define NAME_LENGTH 200
#define NUMBER_OF_SEATS 12

有问题的结构:

//Create the struct (visible to all functions)
struct seat{
int id;
int marker; //0 for available, 1 for occupied
char customerName[NAME_LENGTH];
};
//Declare the array of struct (visible to all functions)
struct seat seats[NUMBER_OF_SEATS];

这是导航程序的菜单

void showNumberOfSeatsAvailable();
void assignASeat();
void deleteSeatAssignment();
void printSeats();
int main(void) {
setbuf(stdout, NULL);

int choice = 1;
int isQuit = 0;
//Initialize seats' ID
for(int i = 0; i < NUMBER_OF_SEATS; i ++){
    seats[i].id = i + 1;
}

printSeats();

//User interaction
do{
    //Step 1: Display the memu
    puts("To choose a function, enter its number label:");
    puts("1). Show number of seats available;");
    puts("2). Assign a customer to a seat;");
    puts("3). Delete a seat assignment;");
    puts("4). Quit");

    //Step 2: accept user input: the choice
    scanf("%d", &choice);

    //Step 3: Execute corresponding performance based on user choice
    switch(choice){
    case 1:
        showNumberOfSeatsAvailable();
        break;
    case 2:
        assignASeat();
        break;
    case 3:
        deleteSeatAssignment();
        break;
    case 4:
        isQuit = 1;
        break;
    default:
        break;
    }
    if(isQuit == 1)
        break;
}while(1 == 1);
return EXIT_SUCCESS;
}

void printSeats(){
}

我认为问题来自

void assignASeat(){
char name[200];
puts("Implement the functionality of assigning a custumer to a seat");
//step 1: check if there is any seat available
 int available;

for(int i = 0; i < NUMBER_OF_SEATS; i ++){

    if(seats[i].marker ==0 ){
        available++;
    }
}
if (available >= 1){ 
    printf("you're in luck! Theres Seats!\n");
for(int i = 0; i < NUMBER_OF_SEATS; i ++){
    if(seats[i].marker ==0){
        seats[i].marker = 1;
        printf("Enter Customer name: ");
        scanf(" %c",  seats[i].customerName[200]);
        printf("        \n");
        seats[i].customerName[200] = name;
        printf( " %c has been assigned a seat\n",name);
        break;
    }
    }
}
else {
    printf("All seats are booked");
}

}
void showNumberOfSeatsAvailable(){
}
void deleteSeatAssignment(){
}

当我尝试设置客户名称时出现问题。以下是导航菜单并选择分配座位后的输出:

Implement the functionality of assigning a custumer to a seat                                                                       
you're in luck! Theres Seats!                                                                                                       
Enter Customer name: John doe                                                                                                          
Segmentation fault 

任何想法为什么我得到这个?我一直在环顾四周,但似乎找到了解决方案。你能帮忙的话,我会很高兴。感谢

1 个答案:

答案 0 :(得分:0)

适合我:

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

#define NAME_LENGTH 200
#define NUMBER_OF_SEATS 12

struct seat {
    int id;
    int marker; //0 for available, 1 for occupied
    char customerName[NAME_LENGTH];
};
struct seat seats[NUMBER_OF_SEATS];

int main() {
    strcpy(seats[1].customerName, "Bob Smith");
    printf("name: %s\n", seats[1].customerName);
    return 0;
}