C_Cpuls 2022-10-09 20:26 采纳率: 100%
浏览 23
已结题

未初始化局部变量zsbd

问题遇到的现象和发生背景

c语言

用代码块功能插入代码,请勿粘贴截图
#include<stdio.h>
#include<Windows.h>
#include<math.h>
#define StackSize 100
//定义栈
struct SeqStack
{
    char data[StackSize];
    int top;
};
//置空栈
void InitStack(struct SeqStack *s)
{
    s->top=-1;
}
//判空栈
int StackEmpty(struct SeqStack *s)
{
    if( s->top==-1)
        return 1;
    else
    {
        printf("栈不为空");
        return 0;
    }
}
//判满栈
int StackFull(struct SeqStack *s)
{
    if(s->top==StackSize-1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
//进栈
void Push(struct SeqStack *s,char x)
{
    if(StackFull(s))
        printf("栈已满");
    else
        s->data[++s->top]=x;
}
//退栈
char Pop(struct SeqStack *s)
{
    if(StackEmpty(s))
        printf("栈已空");
    else
        return s->data[s->top--];
}
//遍历栈
void PrintfStack(SeqStack *s){
    for(int i=0;i<s->top;i++)
        printf("s%",s->data[i]);
}
int main()
{
    struct SeqStack *l;
    InitStack(l);
    Push(l,1);
    Push(l,2);
    Push(l,3);
    Push(l,4);
    Push(l,5);
    PrintfStack(l);
}

运行结果及报错内容

“Pop”: 不是所有的控件路径都返回值
使用了未初始化的局部变量“l”

我的解答思路和尝试过的方法

是没有初始化指针,给他分配内存空间吗?
如果要如何操作,这方面知识盲区

  • 写回答

2条回答 默认 最新

  • qzjhjxj 2022-10-09 23:28
    关注

    修改处见注释,供参考:

    #include<stdio.h>
    #include<stdlib.h>
    //#include<Windows.h>
    //#include<math.h>
    #define StackSize 100
    //定义栈
    struct SeqStack
    {
        char data[StackSize];
        int top;
    };
    //置空栈
    void InitStack(struct SeqStack **s)// (struct SeqStack *s)修改
    {
        (*s)=(struct SeqStack*)malloc(sizeof(struct SeqStack));//修改
        (*s)->top=-1;                //修改
    }
    //判空栈
    int StackEmpty(struct SeqStack *s)
    {
        if( s->top==-1)
            return 1;
        else
        {
            printf("栈不为空");
            return 0;
        }
    }
    //判满栈
    int StackFull(struct SeqStack *s)
    {
        if(s->top==StackSize-1)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    //进栈
    void Push(struct SeqStack *s,char x)
    {
        if(StackFull(s))
            printf("栈已满");
        else
            s->data[++s->top]=x;
    }
    //退栈
    char Pop(struct SeqStack *s)
    {
        if(StackEmpty(s))
            printf("栈已空\n");
        else
            return s->data[s->top--];
    }
    //遍历栈
    void PrintfStack(SeqStack *s){
        for(int i=0;i<=s->top;i++)   //(int i=0;i<s->top;i++)
            printf("%c ",s->data[i]); //printf("s%",s->data[i]);
        printf("\n");                //修改
    }
    int main()
    {
        struct SeqStack* l;
        InitStack(&l);   //修改 InitStack(l);
        Push(l,'1');     //修改
        Push(l,'2');
        Push(l,'3');
        Push(l,'4');
        Push(l,'5');
        PrintfStack(l);
    
        printf("%c",Pop(l)); //修改
        printf("%c",Pop(l));
        printf("%c",Pop(l));
        printf("%c",Pop(l));
        printf("%c",Pop(l));
        printf("%c",Pop(l));
    
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月25日
  • 已采纳回答 10月17日
  • 创建了问题 10月9日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改