Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
518 views
in Technique[技术] by (71.8m points)

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 (作业要求)

求解麻烦了。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

比左右值更重要的问题是:栈中存储的到底是GOBJ还是指向GOBJ的指针?从声明GOBJ* st看你是要存储GOBJ,而从malloc的调用方式上看你又是要存GOBJ*,然后就混乱了。

如果你要存指针,那么st需要声明成GOBJ** stmalloc也不用强制类型转换了,然后那句入栈就可以用数组记号写成:

st->st[st->top] = gobj;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...