gpt4 book ai didi

c++ - 段错误,无法找到任何指针为 NULL 的位置

转载 作者:行者123 更新时间:2023-11-28 00:31:52 25 4
gpt4 key购买 nike

#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;

标准的#includes,定义了原型(prototype)。

struct housingNode * readIn(housingNode * name); 
void displayAll(housingNode * name);

我认为结构构建正确;

struct housingNode
{
int index;
int rent;
int size;
int room;
char currentLocation[101];
char tFeat1[101];
char tFeat2[101];
char tFeat3[101];
char * location;
char * Feat1;
char * Feat2;
char * Feat3;
housingNode * next;
};


int main()
{
housingNode * head;
head = NULL;
readIn(head);
return 0;
}

现在,我知道它必须在这两个函数之一中,并且由于尚未调用第二个函数,我敢打赌它就是这个函数,但我找不到任何地方可以指示我的指针为空,或以这种方式引用其中任何一个。

struct housingNode* readIn(housingNode * name)
{
housingNode * current;
housingNode * temp;
housingNode * head;

head = current;
temp = current;


//file with housing information
ifstream file_in;
file_in.open("housingFile.txt");

for(int i = 0; i < 12; ++i)
{

//starts the building process for the LLL
current = new housingNode;

//reads in the information for the LLL
file_in >> current -> rent;
file_in.ignore(100,';');
file_in >> current -> size;
file_in.ignore(100,';');
file_in >> current -> room;
file_in.ignore(100,';');
file_in.get(current -> currentLocation,100,';');
file_in.ignore(100,';');

//Allocates the needed memory for storing the information into a LLL.
current -> location = new char[strlen(current -> currentLocation)+1];
strcpy(current -> location, current -> currentLocation);
file_in.get(current -> tFeat1,100,';');
file_in.ignore(100,';');
current -> Feat1 = new char[strlen(current -> tFeat1)+1];
strcpy(current -> Feat1, current -> tFeat1);
file_in.get(current -> tFeat2,100,';');
file_in.ignore(100,';');
current -> Feat2 = new char[strlen(current -> tFeat2)+1];
strcpy(current -> Feat2, current -> tFeat2);
file_in.get(current -> tFeat3,100,';');
file_in.ignore(100,'\n');
current -> Feat3 = new char[strlen(current -> tFeat3)+1];
strcpy(current -> Feat3, current -> tFeat3);

temp -> next = current;
temp = temp -> next;
}

//resets the node to null
current -> next = NULL;

//Closes file after reading in information.
file_in.close();

//returns the head pointer
return head;
}

void displayAll(housingNode * name)
{

housingNode * traverse;
housingNode * head;
traverse = head;

cout << "Listing housing information: " << '\n';

for(int i = 0; i < 12; ++i)
{
cout <<"Cost" << traverse -> rent <<'\n';
cout <<"Size: " << traverse -> size <<'\n';
cout <<"Number of Rooms: " << traverse -> room <<'\n';
cout <<"Location: " << traverse -> currentLocation <<'\n';
cout << "Features: " << '\n';
cout <<"#1: " << traverse -> Feat1 <<'\n';
cout <<"#2: " << traverse -> Feat2 <<'\n';
cout <<"#3: " << traverse -> Feat3 << endl;

traverse = traverse -> next;
//Full completed function for displaying all.
}
}

最佳答案

这里有一个问题:

struct housingNode* readIn(housingNode * name)
{
housingNode * current;
housingNode * temp;
housingNode * head;

head = current; // <-----

current 在这里没有初始化,稍后你使用也没有正确初始化的 temp

关于c++ - 段错误,无法找到任何指针为 NULL 的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22553449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com