我遇到了问题。我试图创建一个链接列表,我得到这些错误。我不知道我是否正确传递指针。我甚至无法使我的程序正常运行。
我正在尝试从一个文件中获取数据并将其放入一个没有全局头的链接列表(我必须通过它)(这被认为是head1
)然后,使用我制作的链接列表,我需要制作16个链接列表(创建然后销毁,这是head2
),每个大小为六个字母。恩。接下来的六个,然后是接下来的六个等等。我有点构造,但我不知道从哪里开始。
错误讯息:
无效应用' sizeof'到不完整类型的结构节点' 第85行
请求会员' nextData'在一些不是结构或联合的东西第91行(只是一个例子,这些领域有很多。)
代码:
struct boggleDataNode {
//(line 14) This is the data portion of the node? So we are storing 96 arrays? or 96 nodes?
char data[3];
// (line 16) Is this the pointer variable? This points to the next node in the link list correct?
struct boggleDataNode *nextData;
};
struct boggleDieSideNode {
char dieSideData[3];
struct boggleDieSideNode *nextSide;
};
//creating the function prototypes.
void input(struct boggleDataNode *head);
void addBoggleData(struct boggleDataNode *head, char data);
void displayBoggleData(struct boggleDataNode *head);
//main function
int main()
{
//creating a var to use as a counter
int counter = 0;
struct boggleDataNode *head1 = NULL;
struct boggleDieSideNode *head2 = NULL;
//calls the function that reads data intp a file.
input(head1);
//displays the data from rhe file
displayBoggleData(head1);
//displays the die sides
displayDieSide(head2);
//creates a for loop to create the die sides.
int side, die;
for(die =0; die<16; die++){
//resets the entire link list.
head2 = NULL;
for(side = 0; side<6; side++){
//adds the data to the die side (NOT COMPLETE!)
addDieSideNode(head2, head1);
}
}
}
//this function will handle user imput
void input (struct boggleDataNode *head1)
{
//create data size as well as bounds
char fileData[3];
int bound = 96;
//file pointer and file info
FILE *ips;
ips = fopen("data.txt", "r");
if (ips == NULL)
printf("Please check file!\n");
else {
//for loop to scan through file, and retrive the letters
int i;
for(i=0; i<bound; i++)
fscanf(ips, "%s", fileData);
addBoggleData(head1, fileData);
}
//closes the file system
close(ips);
}
void addBoggleData(struct boggleDataNode *head, char data)
{
//create a helper and temp
struct boggleDataNode *temp,*helper;
//allocate memory (error here)
temp =(struct boggleDataNode *)malloc(sizeof(struct node));
//this sets the data for my temp variable
strcpy(temp.data, data);
if(head==NULL){
//sets the first portion of the link list
head = temp;
temp.nextData = NULL;
//append function 1
}else
{
helper = head;
//(line 70) I dont even know what I am doing. I dont know why temp points to the head, and why I am setting head = temp.
while(helper.nextData != NULL){
helper = helper.nextData;
}
helper.nextData = temp;
}
}
//display function
void displayBoggleData(struct boggleDataNode *head){
//creates a helper var to loop through the link list.
struct boggleDataNode *helper;
helper-head;
if(helper==NULL){
return;
}
//counter to keep track of the data value
int counter=0;
while(helper != NULL){
//prints data
printf(" Data Value %d %s", &counter, helper.data);
counter++;
//moves on
helper = helper.nextData;
}
}
//something that I might never figure out.
void addBoggleSide()
// Incomplete
答案 0 :(得分:1)
首先,don't cast the return value of malloc()
in C。
然后,
但我不知道从哪里开始。
让我通过提供一些指针 (双关语)来帮助你。
在您的代码中出现问题
temp
的类型为struct boggleDataNode *
,因此内存分配语句
temp =(struct boggleDataNode *)malloc(sizeof(struct node));
应该是
temp = malloc(sizeof(struct boggleDataNode ));
或者更确切地说,是一个更好且更健壮的版本
temp = malloc(sizeof*temp); //yes, sizeof operator only needs ()
// when using a datatype as operand
最后一个陈述是独立的,不受temp
类型变化的影响。
temp
是指针类型,
temp.nextData = NULL;
应该是
temp->nextData = NULL;
现在,请检查并更正代码中所有类似问题(如果有)。