【发布时间】:2018-11-14 09:21:46
【问题描述】:
帮忙看看上面写着 //PLEASE TELL ME PROBLEM IN THIS LINE
包括
#include <stdlib.h>
// A linked list node
typedef struct Node
{
int info;
struct Node *link;
}node;
//insert in a linked list
void insert(node* head, int k)
{
node* temp = (node*) malloc(sizeof(node));
if(temp==NULL)
{
printf("malloc was unsuccessfull");
exit(0);
}
else
{
temp->info=k;
temp->link=NULL;
if(head==NULL)
head = temp;
else
{
temp->link=head;
head=temp;
}
}
// This function prints contents of linked list starting from head
void print(node* a)
{
while (a != NULL)
{
printf(" %d ", a->link); //PLEASE TELL ME PROBLEM IN THIS LINE
a = a->link;
}
}
}
void main()
{
/* Start with the empty list */
node* head = NULL;
insert(head, 7);
insert(head, 9);
printf("\n Created Linked list is: ");
print(head); //PLEASE TELL ME PROBLEM IN THIS LINE
}
错误消息
prog.c: In function 'print':
prog.c:40:13: warning: format '%d' expects argument of type 'int', but argument 2 has type 'struct Node *' [-Wformat=]
printf(" %d ", a->link);
^
prog.c: In function 'main':
prog.c:55:3: warning: implicit declaration of function 'print' [-Wimplicit-function-declaration]
print(head);
^
/tmp/cc1HDBBm.o: In function `main':
3192773816853040ef42d0aa4269a062.c:(.text+0xeb): undefined reference to `print'
collect2: error: ld returned 1 exit status
【问题讨论】:
-
您的意思是打印
a->info(这是一个int,可以用%d打印)而不是a->link? -
看起来“%d”需要“int”类型的参数,但参数 2 的类型为“struct Node *”。您不应该打印
info成员而不是指向下一个节点的指针吗? -
正确缩进你的代码,最后一个问题应该很清楚了。
-
函数'print'的隐式声明意味着1)你忘记了stdio.h和2)你在废话模式下使用gcc,而你应该将它用作C编译器:
gcc -std=c11 -pedantic-errors。
标签: c struct linked-list