【发布时间】:2017-02-05 19:53:54
【问题描述】:
我正在尝试获取字符和每个字符的数量并将它们放入链接列表中。正如标题所述,我不断收到有关不兼容指针类型的警告和有关取消引用不完整类型的错误。我认为我说的最后一个是因为 start 为空,但我不明白为什么当我将它分配给 temp 时。请帮忙。
/* Author: Miller Kahihu
Date: 2017-02-01
Program getChars.c
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct linkedList
{
struct Element* start;
int size;
} LinkedList;
typedef struct element
{
char key;
int value;
struct Element* next;
} Element;
int main(int argc, char** argv)
{
LinkedList* list = malloc(sizeof(LinkedList));
list->start = NULL;
list->size = 0;
char* fileName = NULL;
if(argc > 1)
{
fileName = argv[1];
}
else
{
exit(1);
}
FILE* fpIn = fopen(fileName, "r");
if(!fpIn)
{
fprintf(stderr, "file %s could not be opened\n", fileName);
exit(1);
}
int c;
Element* temp = (Element*) malloc(sizeof(Element));
while((c=fgetc(fpIn)) !=EOF)
{
if(list->start == NULL)
{
temp->key = (char) c;
temp->value = 1;
temp->next = NULL;
list->start = temp;
printf("%c kappa\n", list->start->key);
}
else
{
printf("%c\n", (char) c);
}
}
//free(temp);
return 0;
}
这是完整的错误信息
getChars.c: In function ‘main’:
getChars.c:51:25: warning: assignment from incompatible pointer type [enabled by default]
list->start = temp;
^
getChars.c:52:45: error: dereferencing pointer to incomplete type
printf("%c kappa\n", list->start->key);
^
【问题讨论】:
-
似乎有
Element的寄生虫 fwd decl。你应该在链表之前声明你的元素结构。
标签: c