我正在编写一个小型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
}
list.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);
}
}
list.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-&gt; chaFilename来打开我需要加载到数据库中的文件。
我遇到了一个新问题,当我调用解析器函数传递* node作为参考编译器时,抱怨与上面相同。例如:
parser.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;
}
parser.h:
#include "list.h"
#ifndef PARSER_H
#define PARSER_H
int start_parse(FNODEPTR *node);
#endif
GCC输出:
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
有什么想法吗?
提前致谢,对任何英语错误感到抱歉。
答案 0 :(得分:2)
struct sFilename
{
char chaFilename[25];
struct sFilename *next;
};
这应该在list.h文件中定义,在语句之前:
typedef struct sFilename FNODE;
typedef FNODE *FNODEPTR;
EDIT ::
int start_parse(FNODEPTR *node)
应该是int start_parse(FNODEPTR node)