尝试使用现有struct数组时访问读取违规

时间:2017-06-26 17:00:05

标签: c++ arrays struct

我正在为我的班级写一个程序。我的程序应该创建一个乘客列表。显然,必须首先创建列表,并且我的程序中的该选项工作正常。但是,当尝试访问第二个函数(按B)时,在调试器中出现以下错误:

  

ConsoleApplication13.exe中的0x00CD4A76抛出异常:0xC0000005:访问冲突读取位置0x00000000。

我想这意味着我正在尝试读入内存中未分配的区域,因此,我正在使用的struct数组未创建。我很困惑,因为第一个选项确实有效,并且我将相同的指针传递给两个函数,因此指针指向在函数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;
}

有关如何修复此错误的任何说明都会有所帮助。提前谢谢。

0 个答案:

没有答案