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
数组的其他代码中进行类似的更改。
推荐阅读
- xcode - 如何在 xcode 12.2 中退出“历史编辑器”模式?
- vue.js - 在同一个 div vue.js 中显示 2 个元素
- c - 为什么我在 C 中得到这个双重释放或损坏的回溯错误?
- latex - 当我使用 \begin {figure}[H] 时标题消失
- json - 使用python提取JSON数据并遇到keyerror
- javascript - 当模块是`npm link`时,Vue 3组件初始化不正确
- javascript - 将自定义滚动条 CSS 分配给 div 而不是 body
- python - Python Pandas:如何为删除的数据制作标签?
- angular - 如何从 Angular 中的 Route Guards 传递布尔参数
- android - 如何将 Android 设备用作 BLE(低功耗蓝牙)