[Error] base operand of '->' has non-pointer type 'stac

犯错=成长

编写顺序栈时出现下边的提醒

31[Error] base operand of '->' has non-pointer type 'stack'

直接上代码(错误版本):

#include <stdlib.h>
#include <stdio.h>
#define SIZE 1000
typedef struct{
    int data[SIZE];
    int top;
}stack; 


void Init(stack &s){//初始化棧,棧里面放的元素不一定是什么
   s.top=-1;    
} //初始化完成,各种参数也已经确定



void Push(stack &s,int e){
    if(s.top==SIZE-1)printf("已经满了~");
    else
    s.data[++(s.top)]=e;
} 


void  Pop(stack &s,int &e){
    if(s.top==-1)printf("没有元素,无法抛出!");
    e=s.data[(s.top)--];

}

void Print(stack &s){
    while(s.top!=-1)
    printf("%d",s.data[s.top--]);
}



int main(){
    stack s;
    Init(s);
    Push(s,2);
    Push(s,2);
    Push(s,2);
    Print(s);
   return 0;    
}
=============================================================

以下是正确版本:

#include <stdlib.h>
#include <stdio.h>
#define SIZE 1000
typedef struct{
    int data[SIZE];
    int top;
}stack; 


void Init(stack &s){//初始化棧,棧里面放的元素不一定是什么
   s.top=-1;    
} //初始化完成,各种参数也已经确定



void Push(stack &s,int e){
    if(s.top==SIZE-1)printf("已经满了~");
    else
    s.data[++(s.top)]=e;
} 


void  Pop(stack &s,int &e){
    if(s.top==-1)printf("没有元素,无法抛出!");
    e=s.data[(s.top)--];

}

void Print(stack &s){
    while(s.top!= -1)
    printf("%d",s.data[s.top--]);
}



int main(){
    stack s;
    Init(s);
    Push(s,2);
    Push(s,2);
    Push(s,2);  
    Print(s);
   return 0;    
}

反思:之前从来没有太注意 “.”以及”->”之间的区别,今天才发现有多么严重。”.”适用于结构体变量,”->”适用于结构体指针变量!!!!

还有,一定要记得初始化~## 标题 ##

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱码仕1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值