指针有些麻烦。我有一个函数应该返回链表中的节点,该函数应该返回指向变量的指针。我很困惑,看不出我做错了什么!
data.h - 头文件
int add(int num);
struct message *findMessage(int num);
typedef struct list_el message;
data.c - 链接列表。
#include <stdio.h>
#include <stdlib.h>
struct list_el {
int num;
struct list_el *next;
};
typedef struct list_el message;
struct list_el *head, *tail;
int addNode(struct list_el *curr)
{
if(head == NULL) {
head = curr;
} else {
tail->next = curr;
}
curr->next = NULL;
return 0;
}
int add(int num)
{
message *curr;
head = NULL;
curr = (message*) malloc(sizeof(message));
curr->num = num;
addNode(curr);
return 0;
}
message *findMessage(int num)
{
message *curr;
for(curr = head; curr != NULL; curr = curr->next) {
if(curr->num == num) {
return curr;
}
}
return NULL;
}
Main.c - MAIN
#include <stdio.h>
#include "data.h"
int main(void)
{
int num = 2;
add(num);
message *curr = findMessage(num);
return 0;
}
答案 0 :(得分:1)
为什么不在标题中包含struct list_el
的定义?否则main
无法使用它。