私はファイル名(のみ)でリンクリストをロードする小さな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を使用してファイルを開くことはできませんが、DBにロードする必要があります。
参照コンパイラが上記と同じように文句を言うので、* 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
何か案は?
よろしくお願いします。英語の間違いをお詫びします。