C 段错误 - 哪里/什么?

标签 c

我有这个程序,应该负责为乘客分配座位。

我无法弄清楚是什么导致了我的段错误。我正在使用一个结构数组,这可能就是问题所在。

我认为这可能是取消引用某些结构成员的问题,但我找不到哪里。

这是我的代码:

struct seat
{
  // Max Name of 32 chars
  char name[32];

  // Seat Number
  int sNum;

  // Seat taken = 1, open = 0
  int taken;
};

// Function headers
void seat(struct seat plane[][10]);
void mani(struct seat plane[][10]);
void pass(int seat, char name[], int class);

int main()
{
// My airline plane is 6 seats per row, 10 rows
// Row 1/2 are First Class
// Row 3/4 are Business Class

  // Init counter variables to keep track of the number
  // of First Class/Business seats already taken
  // also the user input var
  int input, ticketin, class1 = 0, class2 = 0, class3=0, sNum, i;
  char namein[32];

  // Vars for pass function
  char passname[32];
  int passclass;
  int passseat;

  // Init a 2d array 6 colums 10 rows
  struct seat plane[6][10];

  for (i=0; i<sizeof(plane); i++)
  {
    plane[i]->sNum = i+1;
    plane[i]->taken = 0;
  }

  // Begin user input loop
  // Menu with 3 options:
  // Display the seating chart, indicating taken seats
  // Display the manifest
  // Display a boarding pass - seat number, name, class
  do
  {
    do
    {
      // Prompt user for ticket selection
      printf("Please type 1 for \"First Class\"\n");
      printf("Please type 2 for \"Business Class\"\n");
      printf("Please type 3 for \"Economy Class\"\n");

      scanf(" %d", &ticketin);

      // Check for valid input
      if (ticketin == 1)
      {
        // Check to make sure first class is not full
        if (class1 < 12)
    {
          class1 += 1;
          printf("First class is open!\n");
    } else {
          printf("First class is full, please choose another class\n");
    }

      } else if (ticketin == 2)
      { 
        // Check to make sure business class is not full
        if (class2 < 12)
    {
          class2 += 1;
          printf("Business class is open!\n");
    } else {
          printf("Business class is full, please choose another class\n");

          if (class1 < 12)
      {
            printf("Upgrade to First Class by entering 1");
      }
    }

      } else if (ticketin == 3)
      {
        // Check to make sure business class is not full
        if (class3 < 12)
    {
          class3 += 1;
          printf("Economy class is open!\n");
    } else 
    {
          printf("Economy class is full, please choose another class\n");

          if (class1 < 12)
      {
            printf("Upgrade to First Class by entering 1");
      }

          if (class2 < 12)
      {
            printf("Upgrade to Business Class by entering 2");
      }
    }
      } else
      {
        ticketin = 4;
      }

      // Prompt the user for their name
      printf("Please input your name:\n");
      scanf(" %s", namein);

    } while (ticketin == 4);


    // Handle loading the new passenger into plane array
    switch (ticketin)
    {
      case 1:
        for (i=0; i<12; i)
        {
          if (plane[i]->taken == 0)
          {
            plane[i]->taken = 1;
            strcpy(plane[i]->name, namein);
            sNum = plane[i]->sNum;
          } else
          {
            i++;
          }
        }

      case 2:
        for (i=12; i<24; i)
        {
          if (plane[i]->taken == 0)
          {
            plane[i]->taken = 1;
            strcpy(plane[i]->name, namein);
            sNum = plane[i]->sNum;
          } else
          {
            i++;
          }
        }

      case 3:
        for (i=24; i<60; i)
        {
          if (plane[i]->taken == 0)
          {
            plane[i]->taken = 1;
            strcpy(plane[i]->name, namein);
            sNum = plane[i]->sNum;
          } else
          {
            i++;
          }
        }
    }


    printf("Menu Options: \n");
    printf("(1) Display the seating chart\n");
    printf("(2) Display the passenger manifest\n");
    printf("(3) Display a boarding pass\n");
    printf("(4) Quit\n");

    // Prompt user for their selection
    printf("Please enter your menu selection:\n");
    scanf(" %d", &input);

    // Switch case handling function calls
    switch (input)
    {
      case 1:
        seat(plane);
        break;

      case 2:
        mani(plane);
        break;

      case 3:
        printf("Please input a seat number\n");
        scanf(" %d", &passseat);
        if (passseat < 12)
    {
          passclass = 1;
    } else if (passseat < 24)
    {
          passclass = 2;
    } else
    {
          passclass = 3;
    }
        pass(passseat, plane[passseat-1]->name, passclass);
        break;

      default:
        printf("invalid menu option or quitting\n");
        break;
    }

  } while (input != 4);
}

// Display seating chart function
void seat(struct seat plane[][10])
{
  int i, sNum;
  for (i=0; i<sizeof(plane); i++)
  {
    if (plane[i]->taken == 1)
    {
      printf("Seat %d is taken\n", i++);
    } else
    {
      printf("Seat %d is not taken\n", i++);
    }
  }

}

// Display Manifest function
void mani(struct seat plane[][10])
{
  int i;

  for (i=0; i<sizeof(plane); i++)
  {
    if (plane[i]->taken == 1)
    {
      printf("Passenger %s in seat %d\n", plane[i]->name, i++);
    }
  }

}

// Display boarding pass function
void pass(int seat, char name[], int class)
{
  printf("Boarding pass for %s\n", name);
  printf("Seat Number: %d\n", seat);

  switch (class)
  {
    case 1:
      printf("First Class");
      break;

    case 2:
      printf("Business Class");
      break;

    case 3:
      printf("Economy Class");
      break;
  }
}

最佳答案

可能还有其他错误,但这是最明显的错误:

  for (i=0; i<sizeof(plane); i++)
  {
    plane[i]->sNum = i+1;
    plane[i]->taken = 0;
  }

sizeof(plane)plane 数组中的字节数。这是 10 * 6 * sizeof(struct Seat),因此您正在数组之外写入。如果你想知道数组中元素的数量,你必须用数组的大小除以数组元素的大小:

  for (i=0; i<sizeof(plane)/sizeof(*plane); i++)
  {
    plane[i]->sNum = i+1;
    plane[i]->taken = 0;
  }

但是您的代码仅初始化数组每行中的第一个元素。 由于它是一个二维数组,因此需要嵌套循环。并且您应该使用 . 进行普通成员访问,而不是指针间接访问。

  for (i=0; i<sizeof(plane)/sizeof(plane[0]); i++)
  {
    for (int j = 0; j < sizeof(plane[i])/sizeof(plane[i][0]); j++) 
    {
      plane[i][j].sNum = i+1;
      plane[i][j].taken = 0;
    }
  }

您可以通过定义宏来简化所有 sizeof 内容:

#define ROWS 6
#define COLS 10

然后在数组声明和 for 循环限制中使用这些宏。

需要在循环 plane 数组的其他代码中进行类似的更改。

关于C 段错误 - 哪里/什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55506057/

相关文章:

c++ - 将 8 位数据转换为 3 位数据

c - 找到并删除数组中最长出现的 0 序列

c - 如何以编程方式确定将使用哪个源 IP 地址到达给定目标 IP 地址

c - L1 缓存未命中的成本是多少?

c - Gtk+ 编程风格 : defining widgets

c - 如何在 C 中将数据包写入 TAP 接口(interface)?

c - 在 C 中的 Linux 上的 $PATH 中搜索文件

c - 是否可以使一些用于创建库的函数对调用者不可用?

c - 执行官和我;我怎样才能让它为我工作?

c - UDP 服务器不响应客户端