C 打印堆栈时出错

C 打印堆栈时出错,c,C,我正在尝试使用堆栈实现一个程序。但是堆栈没有显示出来 #include<stdio.h> int size=0,count=1,test=0; struct Car { int registrationNo; char *name; }; struct ParkingLot { struct Car C[10]; }; struct stack { struct ParkingLot P; int top; } st; int

我正在尝试使用堆栈实现一个程序。但是堆栈没有显示出来

#include<stdio.h>
int size=0,count=1,test=0;

struct Car
{
    int registrationNo;
    char *name;
};

struct ParkingLot
{
    struct Car C[10];    
};


struct stack
{
    struct ParkingLot P;
    int top;
} st;

int stfull()
{
    if(st.top >= size-1)
        return 1;
    else
        return 0;
}


void push(struct Car item) {
    st.top++;
    st.P.C[st.top] = item;
}



int stempty() {
    if (st.top == -1)
        return 1;
    else
        return 0;
}

void display() {
    int i;
    if (stempty())
        printf("\nStack Is Empty!");
    else {
    //printf("%d\n",st.top);
        for (i = 0; i<=st.top; i++)
            printf("\n%s", st.P.C[i].name);
    }
}



void Enter_ParkingLot()
{
    struct Car CC;
    int checkFull=stfull();
    if(checkFull==1)
        printf("Parking Lot is FUll\n");
    else
    {
        CC.registrationNo=count;count++;
        char ch[100];
        printf("Enter name of owner\n");
        scanf("%s",ch);

        CC.name=ch;

        push(CC);
    }
}



int main()
{
    printf("Enter size of Parking Lot\n");
    st.top=-1;

    scanf("%d",&size);
    Enter_ParkingLot();
    Enter_ParkingLot();
    display();
    return 0;
}
这是我的作品-

`@
`@
输出中第一个
@
之前有一个空行。

问题:
  • 将汽车名称存储到堆栈上分配的数组中
  • 然后将指向该数组的指针复制到汽车对象
  • 然后,原始数组超出范围
  • 然后尝试通过指针打印不再存在的数组
  • 出现未定义的行为
  • 解决方案:
    您需要使用
    malloc()
    为汽车名称分配内存,并使用
    strcpy()
    将其复制到汽车对象。

    如果您将
    struct car
    中的指针字段指定给局部变量,它将不起作用,您需要像这样重新声明
    struct car

    struct Car
    {
        int registrationNo;
        char name[100];
    };
    
    而不是

    CC.name=ch;
    
    这样做

    strcpy(CC.name, ch);
    
    还有,最好是写

    scanf("%99s",ch);
    
    为了防止溢出
    ch
    ,在您的情况下,这样做会更好

    scanf("%99s", CC.name);
    
    我修正了你的密码

    #include <stdio.h>
    #include <string.h>
    
    struct Car
    {
        int registrationNo;
        char name[100];
    };
    
    struct ParkingLot
    {
        struct Car C[10];
    };
    
    struct stack
    {
        struct ParkingLot P;
        int top;
    } st;
    
    int stfull(int size)
    {
        if(st.top >= size - 1)
            return 1;
        return 0;
    }
    
    void push(struct Car item)
    {
        st.P.C[++(st.top)] = item;
    }
    
    int stempty()
    {
        if (st.top == -1)
            return 1;
        return 0;
    }
    
    void display()
    {
        int i;
        if (stempty() != 0)
            printf("\nStack Is Empty!");
        else {
            for (i = 0 ; i <= st.top ; i++)
                printf("\n%s", st.P.C[i].name);
        }
    }
    
    int Enter_ParkingLot(int count, int size)
    {
        struct Car CC;
    
        if (stfull(size) == 1)
            printf("Parking Lot is FUll\n");
        else
        {
            CC.registrationNo = count;
    
            printf("Enter name of owner\n");
            scanf("%99s", CC.name);
    
            push(CC);
        }
    
        return count + 1;
    }
    
    int main()
    {
        int size = 0, count = 1;
    
        printf("Enter size of Parking Lot\n");
    
        st.top = -1;
    
        scanf("%d", &size);
    
        count = Enter_ParkingLot(count, size);
        count = Enter_ParkingLot(count, size);
    
        display();
        return 0;
    }
    
    #包括
    #包括
    结构车
    {
    国际注册号;
    字符名[100];
    };
    结构停车场
    {
    结构车C[10];
    };
    结构堆栈
    {
    结构停车场P;
    int top;
    }st;
    整数stfull(整数大小)
    {
    如果(st.top>=尺寸-1)
    返回1;
    返回0;
    }
    无效推送(结构车项目)
    {
    st.P.C[++(st.top)]=项目;
    }
    int stempty()
    {
    如果(st.top==-1)
    返回1;
    返回0;
    }
    无效显示()
    {
    int i;
    如果(stempty()!=0)
    printf(“\n堆栈为空!”);
    否则{
    
    对于(i=0;i我现在收到此错误-
    警告:内置函数“strcpy”[默认启用]strcpy(CC.name,ch)的不兼容隐式声明
    @Coder:
    \include
    非常感谢它的工作。你能告诉我以前我的代码中有什么错误吗?我告诉过你,如果你用结构的
    char
    指针字段指向局部变量
    ch
    ,它将始终指向该地址,但该位置的数据将一直存在,直到
    Enter\u ParkingLot()
    函数返回,之后它将消失。
    #include <stdio.h>
    #include <string.h>
    
    struct Car
    {
        int registrationNo;
        char name[100];
    };
    
    struct ParkingLot
    {
        struct Car C[10];
    };
    
    struct stack
    {
        struct ParkingLot P;
        int top;
    } st;
    
    int stfull(int size)
    {
        if(st.top >= size - 1)
            return 1;
        return 0;
    }
    
    void push(struct Car item)
    {
        st.P.C[++(st.top)] = item;
    }
    
    int stempty()
    {
        if (st.top == -1)
            return 1;
        return 0;
    }
    
    void display()
    {
        int i;
        if (stempty() != 0)
            printf("\nStack Is Empty!");
        else {
            for (i = 0 ; i <= st.top ; i++)
                printf("\n%s", st.P.C[i].name);
        }
    }
    
    int Enter_ParkingLot(int count, int size)
    {
        struct Car CC;
    
        if (stfull(size) == 1)
            printf("Parking Lot is FUll\n");
        else
        {
            CC.registrationNo = count;
    
            printf("Enter name of owner\n");
            scanf("%99s", CC.name);
    
            push(CC);
        }
    
        return count + 1;
    }
    
    int main()
    {
        int size = 0, count = 1;
    
        printf("Enter size of Parking Lot\n");
    
        st.top = -1;
    
        scanf("%d", &size);
    
        count = Enter_ParkingLot(count, size);
        count = Enter_ParkingLot(count, size);
    
        display();
        return 0;
    }