如何从c中的头文件外部动态设置头文件中数组的大小?

Howw to dynamically set size of an array in an header file from outside the header file in c?

//stack.h 
struct stack{
    int top;
    char data[];   //set the size of this array
}s;

void init(){    
    s.top = -1;
}

int isFull(){
    return (s.top==MAX-1) ? 1 : 0;
}

 int isEmpty(){
    return (s.top==-1) ? 1 : 0;
}   

void push(char ch){
    if(!isFull()){
        s.data[++s.top] = ch;
    }else{
        printf("\n The stack is full\n");
    }
}

 char pop(){
    return (!isEmpty()) ? s.data[s.top--] : '[=10=]';
}

我想在头文件中实现这个堆栈,并想从 outside.I 中设置数据数组的大小知道这是一个头文件,如果我们更改头变量,它就没有用了,但是还是很好奇

您的结构包含所谓的灵活数组成员。可以通过为结构动态分配 space 加上灵活数组成员的所需大小来创建这样的结构。

struct stack{
    int top;
    char data[];
} *s;

void initStack()
{
    s = malloc(sizeof(struct stack) + MAX * sizeof(char));
}