c - 与 C 中结构化节点的链表相关的奇怪错误消息

标签 c visual-c++ linked-list structure

这应该是一个简单的链接列表问题,但是当我将 input_info() 添加到 main() 函数时,MSVC cl 编译器给我错误消息,如下所示:

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;
}

最佳答案

由于您在 MSVC 中进行编译(严格来说是 C89),因此您不能混合声明和代码,因此

input_info();
struct linked_list *first_ptr=NULL;

不起作用,因为在 input_info(); 之后,编译器会看到一个类型,但它不应该看到这种类型,因为您不能在那里声明任何内容。只需将其更改为:

struct linked_list *first_ptr=NULL;
input_info();

一切都应该正常。

关于c - 与 C 中结构化节点的链表相关的奇怪错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10249836/

相关文章:

c - 这个链表代码是一个好习惯吗?

objective-c - 在 C 中使用 Doxygen 记录变量

c - 我可以做什么来优化以下代码?

C 中的命令行计算器。有什么问题?

c++ - 'for loop' 的终止条件是否在 VC++ 6 中刷新?

c++ - TB_GETBUTTONINFO 在 Windows 7 上失败

visual-c++ - 在 Cygwin shell 中调用 cl.exe(MSVC 编译器)

c++ - 双向链表中的自定义数据类型

c - 递归调用中head变为0后会发生什么?

c - 如何使用c代码打开目录并将数据下载到其中