Vscode编译报错,构建结构体、链表时出现:expected declaration specifiers or ‘...‘ before ‘Link‘gcc

还是在写作业的时候,发现的问题 : )

So, 作业是写一个单链表,所以我打开vscode,输入了如下代码:

#include <stdio.h>
#include <stdlib.h>

struct List
{ 
	int data; 
	struct List *next; 
}Node, *Link;

void insertSorted(Link *head, int newData){
    Link newNode = (Link) malloc(sizeof(Node));  
    if(newNode == NULL){
        printf("Memory Error");
        return;
    }
    // Initialize the node
    newNode->data = newData;
    newNode->next = NULL;

    if(*head == NULL || (*head)->data >= newData){
        newNode->next = *head;
        *head = newNode;
        return;
    }
    
    Link current = *head;
    while(current->next != NULL && current->next->data < newData){
        current = current->next;
    }

    // Insert the node
    newNode->next = current->next;
    current->next = newNode;
}

void printlist(Link head){
    while(head != NULL){
        printf("%d",head->data);
        head = head->next;
    }
    printf("\n");
}

int main(){
    Link head = NULL;
    insertSorted(&head, 1);
    insertSorted(&head, 2);
    insertSorted(&head, 3);
    insertSorted(&head, 4);
    insertSorted(&head, 6);
    printlist(head);
    insertSorted(&head, 5);
    printlist(head);
    return 0;
}

然后报错就悄然发生了。。。

————>

查看错误

————>

expected declaration specifiers or '...' before 'Link'gcc
这段令人琢磨不透的报错就出来了 0.o

------>解决方法:

结构体定义错误,struct List不被gcc理解

应写成------>typedef struct List

And there you go,没有报错,程序正常运行。

新手时用的codeblock根本不会有这个问题啊。

至此,问题解决。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值