c - 在 C 中收到 "member not in struct"错误

标签 c compiler-construction struct

所以我正在编写一个程序,用于从固定格式的文本文件(这是一个目录的反向索引,并按以下方式格式化)中提取单词:

<list> a // word found in indexed files
    0: 1 // in the format <file descriptor>: <number of occurences in file>
    1: 1
</list>
<list> b
    0:1
</list>
<file>
    0: file1.txt //in the format <file descriptor>: <full file path>
    1: testdir/file2.txt
</file>

并将它们存储在链表中。每个单词都存储在一个结构中,该结构包含单词、指向另一个列表对象的指针 [包含文件描述符和出现次数,以及指向文件描述符列表中下一个节点的指针],以及指向下一个节点的指针列表中的节点。

最终,我不会用这些节点创建链表,而是对它们进行哈希处理,以实现搜索功能。

不过,目前我在尝试编译代码时遇到了几个错误。它包含在这里:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "errorchecker.h"
#include "hashtable.h"

int searchthroughfileandcreatenodes(fplist fphead, hashtok hashhead, char * filename)
{   
FILE *fp;
fp = fopen(filename, "r");

if(fp == NULL)                                           //prints an error message if file does not exist
    fatal(": Input file does not exist");

hashtok * temphashhead = &hashhead;
fplist * tempfphead = &fphead;

char word[256];
char* file[256];
int f = 0;

int count = 7;  
int i = 0;                          

                                                                        //This loop is respinsible for creating a linked list of all of the
while (fgets(word, 256, fp) != NULL)                                    //words, the files they appear in (represented by a numerical code),    
{                                                                       //the number of times they appear in each file. It also creates a linked
    if(word[0] == '<')                                                  //list of files represented in the input file, and sotres them. along
    {                                                                   //with the numerical code used to describe them in the input file.
        if(word[1] == 'l')
        {
            while(word[count] != '\0')
            {
                strcpy(file[i], (char*)word[count]);
                count++;
                i++;
            }
            count = 7;
            temphashhead->next = (hashtok)malloc(sizeof(hashtok));
            temphashhead->next->file = (filenumber*)malloc(sizeof(filenumber));
            temphashhead->next->file->next = NULL;
            temphashhead->next->next=NULL;
            temphashhead->next->wordtok =(char*)malloc(sizeof(char) * i + 1);   
            temphashhead = temphashhead->next;                                          //the function enters this loop when it encounters a <l sequence in the
            strcpy(temphashhead->wordtok, file);                                    //input file, which indicates the starts of a new words, and a new
            i = 0;                                                                  //list of files which the word contains. It stores the word into a linked list node,
            fgets(word, 256, fp);                                                   //then creates another linked list coming off of the original node which 
            while(word[0] != '<')                                                   //contains a list of the file number descriptors the word is contained in, and
            {                                                                       //the number of occurences in each file.
                temphashhead->file->fileno = (int) word[1];
                temphashhead->file->numoccurences = (int) word[4];
                temphashhead->file->next = (filenumber*)malloc(sizeof(filenumber));
                temphashhead->file = hashhead->file->next;
                fgets(word, 256, fp);
            }
        }
        else if(word[1] == 'f')                                         //Here, a loop is entered if the '<f' sequence is encountered, which indicates the
        {                                                               //start of the list of files and file descriptors in the input file. Thus, a list of
            while(fgets(word, 256, fp) != NULL)                         //filenames is created, which contains their file descriptors for later reference.
            {
                count = 2;
                {
                    tempfphead->next = (fplist)malloc(sizeof(fplist));
                    tempfphead->next->next = NULL;
                    tempfphead->next->filepath = NULL;
                    tempfphead = tempfphead->next;
                    while(word[count] != NULL)
                    {
                        strcpy(file[i], word[count];
                        i++;
                        count++;
                    }
                    tempfphead->filepath = (char*) malloc(sizeof(char)*i + 1);
                    strcpy(head->filepath, file);
                    i = 0;
                }
            }
        }
        close(fp);
        return 1;
        }   
    }
}

int main(int argc, char *argv[])
{
hashtok hashhead = hashtokcreate();                                     //Creation of new list objects: list to hold the words in the file, and list to hold the filenames in the file.

fplist fphead = fplistcreate();

int stfcn = searchthroughfileandcreatenodes(fphead, head, &argv[1]);

char string[256]; 
char input[256];                              

printf( "search>" );

fgets ( string, 256, stdin );                                       //Here, the fgets function is used in conjunction with the 'stdin' command to retrieve
while(string[0] != 'q')                                             //command line input from the user, and buffer the input into a temporary string. Then, 
{                                                                   //the string is tokenized, and each individual token is copied into a linked list, which,
    char selection[3];                                              //depending on the search parameter (sa or so), will create a new list of the corresponding type
    char * token;                                                   //which will contain the tokens, and will contain empty lists of which filepaths the search
    int i = 0;                                                              //targets are present in, which will be filled in the a later function (sosearch or sasearch, 
    while(i <=1)                                                    //which correpsond to the inputs 'so' and 'sa'.
    {
        strcpy(&selection[i], &string[i]);
        i++;
    }
    if(strcmp(selection, "so") == 0)
    {
        sotoken sohead = socreate();

        socreatelist(&sohead, &string);
        sosearch(&fphead, &head, &sohead);
    }
    else if(strcmp(selection, "sa") == 0)   
    {
        sotoken sahead = sacreate();
        sacreatelist(&sahead, &string); 
        sasearch();
    }
    else
    {
        printf("usage -> search> <so|sa|q> <words to search for>\n/t\t<so>->search for files that contain any combination of the target words\n\t\t<sa>->search for words containing exact string\n\t\t<q>->quit program\n");
    }
    clear(selection);
}
return;
}
    /*hashtokcreate() and fplistcreate() functions create a new sotoken or satoken list     object, and set all intial values to NULL*/

hashtok hashtokcreate()
{
hashtok head = (hashtok)malloc(sizeof(hashtok));
hashtok->file = (filenumber)malloc(sizeof(filenumber));
hashtok->file->next = NULL;
hashtok->next = NULL;
hashtok->wordtok = NULL;
return head;
}

fplist fplistcreate()
{
fplist head = (fplist)malloc(sizeof(fplist));
head->filepath = NULL;
head->next = NULL;
return head;
}

其中包含以下文件“hashtable.h”:

#ifndef HASHTABLE_H
#define HASHTABLE_H

typedef unsigned int (*HashFunctionT) (char* string, int upperbound);

struct filepathlist                         //Will contain the     filepaths represented in the file, and the numerical codes they are represented by.
{
    int pathnumber;
    int numoccurences;
    char * filepath;
    struct filepathlist * next;
};
typedef struct filepathlist * fplist;

struct filenumber_                      //Will contain the file     descriptors of the files that words are found in, and the number of occurences each word     appears in
{                                       //the file
    struct filenumber  * next;
    int fileno;
    int numoccurences;
};
typedef struct filenumber_ * filenumber;

struct Hashtoken                        //Will contain a list of all     of the words present in the input file, and their corresponding appearances in files.
{
    char * wordtok;
    filenumber * file;
    struct Hashtoken * next;
};
typedef struct Hashtoken* hashtok;

struct sotokens                         //Will contain a list of all     of the search targets that were enetered with the parameter 'so', and their corresponding
{                                           //appearances in files.
    char * soword;
    filenumber * files;
    struct sotokens * next;
};
typedef struct sotokens* sotoken;

struct satokens                         //Will contain a list of all     of the search targets that were enetered with the parameter 'sa', and their corresponding
{                                           //appearances in files.
char* saword;
filenumber files;
struct satokens * next;
};
typedef struct satokens* satoken;

fplist fplistcreate();
hashtok hashtokcreate();
sotoken socreate();
satoken sacreate();
sotoken socreatelist(sotoken * head, char * searchtargets);
satoken sacreatelist(satoken * head, char * searchtargets);

#endif

所以每当我尝试编译时,我都会收到一大堆错误。我似乎遇到的主要错误是:

User@root:~/test$ gcc -o s search.c
search.c: In function ‘searchthroughfileandcreatenodes’:
search.c:38:22: warning: cast to pointer from integer of different size
search.c:43:17: error: request for member ‘next’ in something not a structure or union
search.c:44:17: error: request for member ‘next’ in something not a structure or union
search.c:45:17: error: request for member ‘next’ in something not a structure or union
search.c:46:17: error: request for member ‘next’ in something not a structure or union
search.c:47:17: error: request for member ‘next’ in something not a structure or union
search.c:48:32: error: request for member ‘next’ in something not a structure or union
search.c:49:24: error: request for member ‘wordtok’ in something not a structure or union
search.c:49:5: warning: passing argument 2 of ‘strcpy’ from incompatible pointer type
/usr/include/string.h:128:14: note: expected ‘const char * __restrict__’ but argument is of type ‘char **’
search.c:54:18: error: request for member ‘file’ in something not a structure or union
search.c:55:18: error: request for member ‘file’ in something not a structure or union
search.c:56:18: error: request for member ‘file’ in something not a structure or union
search.c:57:18: error: request for member ‘file’ in something not a structure or union
search.c:57:41: error: request for member ‘next’ in something not a structure or union
search.c:67:17: error: request for member ‘next’ in something not a structure or union
search.c:68:17: error: request for member ‘next’ in something not a structure or union
search.c:69:17: error: request for member ‘next’ in something not a structure or union
search.c:70:30: error: request for member ‘next’ in something not a structure or union

我试过如何声明我的结构,以及如何创建我的列表,但我似乎无法弄清楚是什么导致了这些错误。我知道要筛选很多代码,但如果有人能指出我做错了什么,我将非常感激。

谢谢。

最佳答案

hashtok 是一个指针。所以temphashhead是一个指向指针的指针。所以你必须说 (*temphashhead)->next

(这也是完全没有意义的,因为 temphashhead 只指向一个局部变量,您永远不需要重新设置指针。所以只需说 hashtok temphashhead = hashhead;。 )

关于c - 在 C 中收到 "member not in struct"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8116190/

相关文章:

c - 为什么在为存储要在程序中使用的 main 参数的指针变量赋值之前不分配空间?

c++ - 重新排列数组元素

swift - 如何在 Swift 中实现管理 UserDefaults 的键值对的通用结构?

java - 创建泛型集合数组

c++ - 如何检查C++编译器生成的代码?

c - uthash 将新条目添加到 <struct, struct> HashMap

c - 当结构体开头的名称和结构体末尾的名称不同时,这意味着什么?

c - 当无法执行管道时如何在 Linux 上创建自解压可执行文件

c - 二叉树和快速排序?

compiler-construction - 实现编译器和解释器有什么区别?