C++ 尝试使用现有结构数组时发生访问读取冲突
我正在为我的班级写一个程序。我的程序应该创建一个乘客列表。显然,必须首先创建列表,并且我的程序中的该选项工作正常。但是,当尝试访问第二个函数(按B)时,调试器中出现以下错误: ConsoleApplication13.exe中0x00CD4A76处引发异常:0xC0000005:访问冲突读取位置0x00000000 我猜这意味着我试图读取内存中的未分配区域,因此,我使用的结构数组没有创建。我很困惑,因为第一个选项不起作用,我将同一个指针传递给两个函数,因此指针指向在函数A中创建的数组C++ 尝试使用现有结构数组时发生访问读取冲突,c++,arrays,struct,C++,Arrays,Struct,我正在为我的班级写一个程序。我的程序应该创建一个乘客列表。显然,必须首先创建列表,并且我的程序中的该选项工作正常。但是,当尝试访问第二个函数(按B)时,调试器中出现以下错误: ConsoleApplication13.exe中0x00CD4A76处引发异常:0xC0000005:访问冲突读取位置0x00000000 我猜这意味着我试图读取内存中的未分配区域,因此,我使用的结构数组没有创建。我很困惑,因为第一个选项不起作用,我将同一个指针传递给两个函数,因此指针指向在函数A中创建的数组
#include <iostream>
#include <string>
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
struct date
{
int year;
int day;
int month;
};
struct seat
{
int row;
char place;
};
struct pass
{
char * passname;
date bookdate;
seat location;
};
int lastindex;
int initList(pass *p, int x);
int addPass(pass *p, date *D, pass *newpass, int length);
void main()
{
pass *p = {};
int length = 0;
char choice;
do {
cout << "Welcome to FlightDesigner2017.\nEnter one of the following keys to continue:\nA: Create a list of passengers.\nB: Add a passenger to the flight.\n";
cin >> choice;
switch (choice)
{
case 'A':
{
int x;
cout << "How many passengers are on your flight? \n";
cin >> x;
length = initList(p, x);
break;
}
case 'B':
{
pass *newpass=0;
date *D = 0;
switch (addPass(p, D, newpass, length))
{
case '1':
cout << "Passenger successfully added."; break;
case '-3':
cout << "No seats available."; break;
case '-1':
cout << "Seat taken. Try again."; break;
case '-2':
cout << "Passenger is already on the flight."; break;
}
break;
}
}
}
while (choice=!0);
}
int addPass(pass *p, date *D, pass *newpass, int length)
{
#define TAKEN -1
#define SAMENAME -2
#define NOSEATS -3
#define SUCCESS 1
for (int init = 0; init < length; init++)
{
int counter=0;
for (int j = 0; j < length; j++) //empty seats check
{
if (strcmp(p[j].passname , NULL)!=0)
counter++;
}
if (counter == length)
return NOSEATS;
cout << "Enter a seat and row (seat A to F, row 1 to 50): \n"; //taken check
cin >> newpass->location.place >> newpass->location.row;
cout << "\nWhat is the flight date (DD/MM/YYYY)? \n";
cin >> D->day >> D->month >> D->year;
for (int k = 0; k < length; k++)
{
if (D->day == p[k].bookdate.day && D->month == p[k].bookdate.month && D->year == p[k].bookdate.year
&& p[k].location.place == newpass->location.place && p[k].location.row == newpass->location.row)
return TAKEN;
}
cout << "What is the passenger name? \n"; // name check
cin >> newpass->passname;
for (int i = 0; i < length; i++)
{
if (strcmp(newpass->passname,p[i].passname)==0)
return SAMENAME;
}
strcpy(newpass->passname, p[init].passname);
p[init].location.place = newpass->location.place;
p[init].location.row = newpass->location.row;
p[init].bookdate.year = D->year;
p[init].bookdate.month = D->month;
p[init].bookdate.day = D->day;
char ans;
cout << "Enter another passenger? (Y/N)\n";
cin >> ans;
if (ans == 'N')
return SUCCESS;
}
return SUCCESS;
}
int initList(pass *p, int length)
{
p = new pass[length];
for (int i = 0; i < length; i++)
p[i].passname = NULL;
return length;
}
#包括
#包括
#定义\u CRT\u安全\u无\u警告
使用名称空间std;
结构日期
{
国际年;
国际日;
整月;
};
结构座椅
{
int行;
焦位;
};
结构通道
{
字符*密码名;
记账日期;
座位位置;
};
int-lastindex;
int initList(通过*p,int x);
int addPass(pass*p,date*D,pass*newpass,int length);
void main()
{
通过*p={};
整数长度=0;
字符选择;
做{
选择;
开关(选择)
{
案例“A”:
{
int x;
cout>x;
长度=初始列表(p,x);
打破
}
案例“B”:
{
pass*newpass=0;
日期*D=0;
开关(addPass(p、D、newpass、length))
{
案例“1”:
cout newpass->location.row;
cout>D->day>>D->month>>D->year;
for(int k=0;kday==p[k]。bookdate.day&&D->month==p[k]。bookdate.month&&D->year==p[k]。bookdate.year
&&p[k].location.place==newpass->location.place&&p[k].location.row==newpass->location.row)
回程;
}
cout>newpass->passname;
for(int i=0;ipassname,p[i].passname)==0)
返回SAMENAME;
}
strcpy(newpass->passname,p[init].passname);
p[init].location.place=newpass->location.place;
p[init].location.row=newpass->location.row;
p[init].bookdate.year=D->year;
p[init].bookdate.month=D->month;
p[init].bookdate.day=D->day;
查尔安斯;
cout>ans;
如果(ans=='N')
回归成功;
}
回归成功;
}
int initList(pass*p,int length)
{
p=新焊道[长度];
for(int i=0;i
任何关于如何修复此错误的解释都会有所帮助。请提前感谢。我想这意味着我试图读取内存中未分配的区域,这看起来像是在对空指针进行解频。哦,很容易发现。
p
是空指针。int initList(pass*p,int x);
不会更改main中p
的值,因为您是按值传递指针的。请将其更改为int initList(pass*&p,int x);
以进行修复。同时使addPass()生效
在继续之前检查p是否为空。是否允许您使用std::string
和std::unique\u ptr
?这会让您的生活更轻松。@Tommy“…似乎出于某种奇怪的原因没有取悦我的老师。”哈哈,您是个正直的人,