c++ - c语言实现顺序栈出错【操作符"="左边的值须为 “左值“
本文介绍了c++ - c语言实现顺序栈出错【操作符"="左边的值须为 “左值“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
数据结构
typedef struct GOBJ{
bool hoving;
int width;
int height;
int x;
int y;
IMAGE_PNG img;
}GOBJ;
typedef struct STACK{
int msize; // max size
int top; // stack top
GOBJ* st; // stack arr
}STACK;
// 顺序栈初始化
void arrStk(STACK* st,int size){
st->st = (GOBJ*)malloc(sizeof(GOBJ*)*size);
st->msize = size;
st->top = -1;
};
bool arrStkPush(STACK* st,GOBJ* gobj){
if(st->top == st->msize-1){
printf("the stk is full");
return false;
}else{
st->top = st->top + 1;
st->st + st->top = gobj; //!!!本行报错(~ ̄(OO) ̄)ブ
return true;
}
};
编译过程中报错:error C2106: '=' : left operand must be l-value
环境:vc++6 (作业要求)
求解麻烦了。
解决方案
比左右值更重要的问题是:栈中存储的到底是GOBJ
还是指向GOBJ
的指针?从声明GOBJ* st
看你是要存储GOBJ
,而从malloc
的调用方式上看你又是要存GOBJ*
,然后就混乱了。
如果你要存指针,那么st
需要声明成GOBJ** st
,malloc
也不用强制类型转换了,然后那句入栈就可以用数组记号写成:
st->st[st->top] = gobj;
这篇关于c++ - c语言实现顺序栈出错【操作符"="左边的值须为 “左值“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文