我一直在尝试修复我在编译该程序时遇到的几个错误,但似乎找不到任何解决方案。这些是实际代码后发生的以下错误。大多数错误是“unary *”问题,尽管我认为这是使用它的正确方法。如果有人能指出我正确的方向,那将非常有帮助。
testReadLinkedList.c:13: warning: no semicolon at end of struct or union
testReadLinkedList.c: In function ‘insertListElement’:
testReadLinkedList.c:31: error: incompatible types in assignment
testReadLinkedList.c: In function ‘readFile’:
testReadLinkedList.c:43: warning: comparison between pointer and integer
testReadLinkedList.c: In function ‘countList’:
testReadLinkedList.c:51: error: invalid initializer
testReadLinkedList.c:54: error: invalid operands to binary !=
testReadLinkedList.c:56: error: invalid type argument of ‘unary *’
testReadLinkedList.c: In function ‘printList’:
testReadLinkedList.c:65: error: invalid initializer
testReadLinkedList.c:70: error: invalid type argument of ‘unary *’
testReadLinkedList.c:71: error: invalid type argument of ‘unary *’
testReadLinkedList.c:72: error: invalid type argument of ‘unary *’
testReadLinkedList.c:73: error: invalid type argument of ‘unary *’
testReadLinkedList.c:81: error: invalid type argument of ‘unary *’
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <float.h>
typedef struct LinkedListNode {
int red;
int green;
int blue;
char function[128];
struct LinkedListNode* next
} LinkedListNode;
typedef struct {
LinkedListNode* head;
} Linkedlist;
void initLinkedList(Linkedlist* listIn) {
listIn = (Linkedlist*)malloc(sizeof(Linkedlist));
(*listIn).head = NULL;
}
void insertListElement(Linkedlist* listIn, int redIn, int greenIn, int blueIn, char* functionIn) {
LinkedListNode* newNode = (LinkedListNode*)malloc(sizeof(LinkedListNode));
(*newNode).red = redIn;
(*newNode).green = greenIn;
(*newNode).blue = blueIn;
(*newNode).function = functionIn;
(*newNode).next = (*listIn).head;
(*listIn).head = newNode;
}
void readFile(FILE* fileIn, Linkedlist* listIn) {
char tempLine[128];
int tempRed, tempGreen, tempBlue;
char tempFunction[128];
while(fgets(tempLine, 128, fileIn) != EOF) {
sscanf(tempLine, "%d %d %d %[^\n]", &tempRed, &tempGreen, &tempBlue, tempFunction);
insertListElement(listIn, tempRed, tempGreen, tempBlue, tempFunction);
}
}
int countList(Linkedlist* listIn) {
LinkedListNode currNode = (*listIn).head;
int size = 0;
while(currNode != NULL) {
size++;
currNode = (*currNode).next;
}
return size;
}
void printList(Linkedlist* listIn) {
int i, tempRed, tempGreen, tempBlue;
char tempFunction[128];
LinkedListNode currNode = (*listIn).head;
int listSize = countList(listIn);
for(i = 0;i < listSize; i++) {
tempRed = (*currNode).red;
tempGreen = (*currNode).green;
tempBlue = (*currNode).blue;
tempFunction = (*currNode).function;
printf("Red: %d\n",tempRed);
printf("Green: %d\n",tempGreen);
printf("Green: %d\n",tempBlue);
printf("Function: %s\n",tempFunction);
print("t\n");
currNode = (*currNode).next;
}
}
int main(int argc, char** argv) {
FILE* f1;
Linkedlist* list1;
f1 = fopen(argv[1], "r");
if(f1 == NULL) {
printf("Error: could not open file");
} else {
initLinkedList(list1);
readFile(f1, list1);
}
}
最佳答案
这里:
(*newNode).function = functionIn;
你需要strcpy
functionIn 到(*newNode).function
,你不能简单地赋值给它。另外,在这一行:
while(fgets(tempLine, 128, fileIn) != EOF)
fgets
错误时不返回 EOF 而是 NULL,所以你需要做:
while (fgets(tempLine, 128, fileIn))
在这一行:
LinkedListNode currNode = (*listIn).head;
head
是一个指针,需要固定currNone
的类型:
LinkedListNode *currNode = listIn->head;
最后,您需要更改您的初始化函数:
void initLinkedList(Linkedlist **listIn) {
*listIn = (Linkedlist *)malloc(sizeof(Linkedlist));
(*listIn)->head = NULL;
}
和调用:
initLinkedList(&list1);
关于c - 无法编译链表程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26507069/