这假设是一个简单的链接列表问题,但是当我将input_info()添加到main()函数时,MSVC cl编译器给了我这样的错误msg:
syntax error : missing ';' before 'type'
error C2065: 'first_ptr' : undeclared identifier
warning C4047: 'function' : 'struct linked_list *' differs in levels of indirection from 'int '
warning C4024: 'print_list' : different types for formal and actual parameter 1
我不明白为什么编译器向我显示这样的错误......请让我了解为什么 MSVC 6.0 编译器会给我这样的错误消息。
/*
*
* this program is a simple link list which will have add(),
* remove(), find(), and tihs link list will contains the student
* information.
*
*/
#include <stdlib.h>
#include <stdio.h>
//this ppl structure student holds student info
typedef struct
{
char *name;
int height;
int weight;
}ppl;
//this structure is the link list structure
typedef struct linked_list
{
ppl data;
struct linked_list *next_ptr;
} linked_list;
//this function will print the link list in a reversed order
void print_list(linked_list *a)
{
linked_list *llp=a;
while (llp!=NULL)
{
printf("name: %s, height: %d, weight: %d n",llp->data.name,llp->data.height,llp->data.weight);
llp=llp->next_ptr;
}
}
//this function will add ppl info to the link list
void add_list(ppl a, linked_list **first_ptr)
{
//new node ptr
linked_list *new_node_ptr;
//create a structure for the item
new_node_ptr=malloc(sizeof(linked_list));
//store the item in the new element
new_node_ptr->data=a;
//make the first element of the list point to the new element
new_node_ptr->next_ptr=*first_ptr;
//the new lement is now the first element
*first_ptr=new_node_ptr;
}
void input_info(void)
{
printf("Please input the student info!n");
}
int main()
{
ppl a={"Bla",125,11};
input_info();
//first node ptr
struct linked_list *first_ptr=NULL;
//add_list(a, &first_ptr);
printf("name: %s, height: %d, weight: %d n",a.name,a.height,a.weight);
//print link list
print_list(first_ptr);
return 0;
}
由于您使用的是严格的 C89 MSVC,因此不能混合声明和代码,因此
input_info();
struct linked_list *first_ptr=NULL;
不起作用,因为在input_info();
之后编译器会看到一个类型,它不应该看到,因为你不能在那里声明任何内容。只需将其更改为:
struct linked_list *first_ptr=NULL;
input_info();
一切都应该正常。
更改为
ppl a={"Bla",125,11};
//first node ptr
struct linked_list *first_ptr=NULL;
input_info();