0
我正在寫一個加載帶有文件名(僅)的鏈接列表的小型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-> chaFilename打開我需要加載到數據庫的文件。
我得到一個新的問題,當我調用解析函數傳遞*節點作爲參考編譯器抱怨與上述相同。例如:
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
任何想法?
在此先感謝,並對任何英語錯誤表示歉意。
我不認爲一個' extern'會這樣做,因爲main.c引用了僅在list.c文件中定義的成員'node-> chaFilename'。 –
@JoachimIsaksson嗯,我必須編譯程序,看看..我認爲你是對的! –
@JoachimIsaksson你是對的。修改答案!謝謝一堆! :-) –