expected primary-expression before xx token错误处理

expected primary-expression before xx token

这个 xx 一般指运算符,比如++

错误的原因是:把类型(type)当成变量来用了(variable)

如下面例子,List *head是一种类型,错误使用。

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

#define num_xuehao 6
#define num_name 10

typedef struct _item{
	int s;
	char xue[num_xuehao];
	char nam[num_name];
	struct _item *next;
}Item;

typedef struct _list{
	Item *last;
	Item *head;
}List;

void add(List *plist,char *xuehao,char *name,int score);//将新输入的项目连接在链表的后面 

int main()
{
	List *head = NULL;
	List *last = NULL;
}

void add(List *head,List *last,char *xuehao,char *name,int score)
{	
	//连接在链表后面 ,新加入的部分的每一个小点都要有初始化 
	Item *p=(Item*)malloc(sizeof(Item));
	p->next = NULL;
	p->s = score;
	strcpy(p->xue ,xuehao);
	strcpy(p->nam ,name);
//	printf("%s %s %d\n",p->nam,p->xue,p->s);
	
	//find the last
	if(List->last){
		while(List->last->next){
			List->last=List->last->next;
		}
		List->last->next = p;
	} else{
		List->last = p;
		List->head = p;
	}	
}

正确的应该是下面这样。

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

#define num_xuehao 6
#define num_name 10

typedef struct _item{
	int s;
	char xue[num_xuehao];
	char nam[num_name];
	struct _item *next;
}Item;

typedef struct _list{
	Item *last;
	Item *head;
}List;

void add(List *plist,char *xuehao,char *name,int score);//将新输入的项目连接在链表的后面 

int main()
{
	List plist;
	plist.head = NULL;
	plist.last = NULL;
}

void add(List *plist,char *xuehao,char *name,int score)
{	
	//连接在链表后面 ,新加入的部分的每一个小点都要有初始化 
	Item *p=(Item*)malloc(sizeof(Item));
	p->next = NULL;
	p->s = score;
	strcpy(p->xue ,xuehao);
	strcpy(p->nam ,name);
//	printf("%s %s %d\n",p->nam,p->xue,p->s);
	
	//find the last
	if(plist->last){
		while(plist->last->next){
			plist->last=plist->last->next;
		}
		plist->last->next = p;
	} else{
		plist->last = p;
		plist->head = p;
	}	
}

 

  • 11
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值