我如何在结构中声明一个数组

how do i declare an array in a struct

我做了这段代码。我也尝试在结构中声明数组。但我不断收到错误消息。有人可以帮助我 :) 谢谢。祝你有个愉快的一天。

#include<stdio.h>
struct stack
{
    int arr[5];
    int top;
}st;


void push (int ele)
{
    st.arr[st.top]=ele;
    st.top++;
}
int pop()
{
    int item=st.arr[st.top];
    st.top--;
    return item;
}
void display()
{
    int i;
    for(i=4;i>=0;i--)
    {
        printf("%d\t",st.arr[i]);
    }
}
int main()
{
   st.arr[5]={3,6,8,7,5};
   display();
}

这行不通:

st.arr[5]={3,6,8,7,5};

因为你不能一次直接赋值给整个数组。但是,您可以做的是初始化结构及其在定义时包含的数组:

struct stack
{
    int arr[5];
    int top;
} st = { {3,6,8,7,5}, 5};