c - 取消引用指向不完整类型的指针 - 列表

标签 c list pointers

我正在编写一个小型 C 程序,它(仅)加载带有文件名的链接列表。我将遍历这个列表并解析每个文件名的内容。

编译时出现以下错误:

main.c: In function 'main':
main.c:25: error: dereferencing pointer to incomplete type
main.c:26: error: dereferencing pointer to incomplete type
make: *** [main.o] Error 1

main.c:

#include <stdio.h>
#include <stdlib.h>

#include "list.h"

int main(int argc, char *argv[])
{
    FNODEPTR node = NULL;

    node = create_filename_list();

    add_filename_node(&node, "file1.txt");
    add_filename_node(&node, "file2.txt");

    //Start Logger
    //Watchdog
    //Warehouse 

    //print_filename_list((struct sFilename *)node);

    while(node!=NULL)
    {
        printf("%s\n",node->chaFilename); //Line 25
        node=node->next; //Line 26
    }

    destroy_filename_list(node);

    exit(0); //0 SUCCESS - 1 FAIL
}

列表.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#include "list.h"

struct sFilename
{
    char chaFilename[25];
    struct sFilename *next;
};

FNODEPTR create_filename_list()
{
    FNODEPTR node = NULL;

    node = (FNODEPTR) malloc(sizeof (struct sFilename)); // Alocando um nó do tipo sFilename

    return node;
}

void add_filename_node(FNODEPTR *fnFilename, char *chaFilename)
{
    FNODEPTR head = *fnFilename; // head recebe o conteúdo de anAction. head aponta para o mesmo endereço que fnFilename
    FNODEPTR newNode;

    newNode = (FNODEPTR) malloc(sizeof (struct sFilename)); // Alocando um nó do tipo sFilename

    strcpy(newNode->chaFilename, chaFilename); // Copiando o conteúdo de chaFilename para newNode->chaFilename.

    newNode->next = head; // next do novo nó aponta para o início da lista (fnFilename)

    head = newNode; //head é igual a newNode. head recebe o endereço de memória de newNode. head está na mesma posição que newNode
    *fnFilename = head; //o conteúdo de anAction é igual a posição de head
}

void print_filename_list(struct sFilename *fnFilename)
{
   if (fnFilename == NULL)
          printf("Nao ha arquivos ftp a serem carregados\n");
   else {
          printf("Os arquivos que serao carregados no T020 sao:\n");
          while (fnFilename != NULL) {
                 printf("\t- %s\n", fnFilename->chaFilename);//fnFilename->chaFilename);
                 fnFilename = (*fnFilename).next;//fnFilename->next;
          }
   }
}

void destroy_filename_list(FNODEPTR fnFilename)
{
    if(fnFilename != NULL)
    {
        free(fnFilename);
    }
}

列表.h:

#ifndef LIST_H
#define LIST_H

typedef struct sFilename FNODE;
typedef FNODE *FNODEPTR;

FNODEPTR create_filename_list();
void add_filename_node(FNODEPTR *fnFilename, char *chaFilename);
void remove_filename_node(FNODEPTR *fnFilename);
void print_filename_list(struct sFilename *);
void destroy_filename_list(FNODEPTR fnFilename);

#endif

此列表将作为对其他函数(模块)的引用传递,这些函数(模块)将解析内容并将内容加载到数据库中。虽然我无法使用 node->chaFilename 打开我需要加载到数据库中的文件。

我遇到了一个新问题,当我调用解析器函数传递 *node 作为引用编译器时,提示与上面相同。示例:

解析器.c:

//Built-in libs
#include <stdio.h>
#include <stdlib.h>

//Homemade libs
#include "parser.h"
#include "list.h"

int start_parse(FNODEPTR *node)
{
    if(node!=NULL)
    {
        printf("Node is not null\n");
        while(node!=NULL)
        {
            printf("\tNode address %x\n", (unsigned int)node);
            node=node->next;
        };
    }

    return 1;
}

解析器.h:

#include "list.h"

#ifndef PARSER_H
#define PARSER_H
int start_parse(FNODEPTR *node);
#endif

海湾合作委员会输出:

gcc -Wall   -c -o parser.o parser.c
parser.c: In function 'start_parse':
parser.c:16: warning: cast from pointer to integer of different size
parser.c:17: error: request for member 'next' in something not a structure or union
make: *** [parser.o] Error 1

有什么想法吗?

提前致谢,对于任何英语错误,我们深表歉意。

最佳答案

struct sFilename
{
    char chaFilename[25];
    struct sFilename *next;
};

这应该在 list.h 文件中的语句之前定义:

typedef struct sFilename FNODE;
typedef FNODE *FNODEPTR;

编辑::

int start_parse(FNODEPTR *node) 应该是 int start_parse(FNODEPTR node)

关于c - 取消引用指向不完整类型的指针 - 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14506256/

相关文章:

C- 如果元素的索引大于动态分配的数组大小,它会返回 null 吗?

c - 关于数组和指针

c# - 如何将数组的字符串复制到列表<string>?

c - 构建并打印一个链表,其中每个节点代表 C 中的一个链表

python - 创建给定字符串和给定字典中所有单词的列表

c - 通过进程在 C 中打开文件

关于数据结构我们何时应该使用 "single-pointer"以及何时使用 "pointer-to-pointer"的困惑

c - Visual Studio 无法在 32 位上链接,但可以在 64 位上链接

c++ - 类 TESTDLL_LIBSHARED_EXPORT TestDLL_lib

c++ - 非阻塞套接字上的 select()、recv() 和 EWOULDBLOCK