c - 传递链接列表的指针

标签 c pointers struct compiler-errors sizeof

我遇到了问题。我正在尝试创建一个链接列表,但收到这些错误。我不知道我是否正确传递了指针。我什至无法让我的程序正确运行。

我试图从文件中获取数据并将其放入没有全局头的链接列表中(我必须传递它)(这被认为是head1)然后,使用链接我制作的列表中,我需要制作 16 个链接列表(创建然后销毁,这是 head2),每个链接列表大小为 6 个字母。前任。接受前六个,然后是接下来的六个等等。我已经构建了它,但我不知道从哪里开始。

错误消息:

invalid application of 'sizeof' to incomplete type 'struct node' line 85

request for member 'nextData' in something not a structure or union line 91 ( just an example, there are many of these areas.)

代码:

  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

最佳答案

首先也是最重要的,don't cast the return value of malloc() in C 。 然后,

but I dont know where to start.

让我通过提供一些提示来帮助您(双关语)

谈到代码中的问题,

  1. 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 类型变化的影响。

  2. temp 是指针类型,

     temp.nextData = NULL;
    

    应该是

    temp->nextData = NULL;
    

现在,请检查并更正您代码中的所有类似问题(如果有)。

关于c - 传递链接列表的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31081386/

相关文章:

检查 char* 是否为小于 0 的数字

c - 如何在 Dev C++ 中对齐字符串

c - 循环被忽略 - 指令仅执行一次

c - C中的未知值链表

c++ - 反转链表(通过递归)不起作用

tomcat - 如何将 hdiv 与多个模块集成(在同一个 tomcat 中运行)

c - C中的FOR循环,条件部分

C 数组指针用法说明

c - 查找结构数组中的记录数

struct - 用另一个结构分配结构