【问题标题】:c- Segmentation Fault when trying to add to char array in Structc-尝试添加到结构中的字符数组时出现分段错误
【发布时间】:2018-08-21 05:43:48
【问题描述】:

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

#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 

知道我为什么会得到这个吗?我一直在环顾四周,但似乎可以找到解决方案。你能帮忙的话,我会很高兴。谢谢

【问题讨论】:

  • 席位[i].customerName[200] = 姓名 ....
  • available 未初始化
  • 您不能将一个字符串分配给另一个字符串。使用strcpy 从一个字符串复制到另一个字符串。
  • 由于您并不真正关心有多少座位可用,只关心是否有座位,建议您在发现第一个空座位后打破for 循环座位。此外,您可以 #include &lt;stdbool.h&gt; 并将其更改为 bool available
  • scanf(" %c", seats[i].customerName[200]); 函数scanf 需要一个地址作为参数来存储字符串。您提供char。将字符的值视为地址很可能不是一个好主意。索引 200 也超出范围。同样对于字符串,您需要格式说明符"%s"。你有很多未定义的行为。

标签: c arrays struct segmentation-fault structure


【解决方案1】:

您不能在 C 中分配 seats[i].customerName[200] = name。要在 c 中复制字符串值,您应该使用 strcpy() 标准库函数。

  char name[200];
  printf("Enter the name:\n");
  scanf("%s",name);
  strcpy(seats[i].customerName,name);
  .
  . 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多