在 C 中收到“成员不在结构中”错误

问题描述 投票:0回答:2

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

<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

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

谢谢。

c struct
2个回答
4
投票

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

(这也是完全没有意义的,因为

temphashhead
只指向一个局部变量,并且你永远不需要重新定位指针。所以只需说
hashtok temphashhead = hashhead;
。)


2
投票

这就是为什么像这样使用 typedef 是不好。他们隐藏意图。当您需要时输入

*
并不难。

跳出来的是这样的:

int searchthroughfileandcreatenodes(fplist fphead, hashtok hashhead, 
                                    char * filename)

hashtok
struct Hashtoken*

的类型定义

然后你这样做:

hashtok * temphashhead = &hashhead;

如果你去掉 typedef 是:

sturct Hashtoken **temphashhead = &hashhead;

您现在有一个指向指针,然后您尝试通过以下方式将其用作指针:

temphashhead->next

随之而来的是编译器错误。

© www.soinside.com 2019 - 2024. All rights reserved.