简体   繁体   English

静态结构初始化C

[英]Static structure initialization C

i read already some answers on stackoverflow but I don't know why it still doesn't work : 我已经阅读了关于stackoverflow的一些答案,但是我不知道为什么它仍然不起作用:

typedef struct gnl_struct {
    char        *data;
    int         where;
    int         status;
}               t_gnl;

void display_elem(t_gnl tab, int nbr)
{
    printf("tab[%d]\n", nbr);
    printf("tab.where == %d\n", tab.where);
    printf("tab.status == %d\n", tab.status);

    return ;
}

int     main()
{
    static t_gnl    tab[1000] = {{ "toto", 0, 2 }} ;

    display_elem(tab[3], 3);

    return (0);
}

the result is : 结果是:

tab[3]
tab.where == 0
tab.status == 0

In your code, you've (yourself) initalized only tab[0] and you're passing tab[3] . 在您的代码中,(您自己)只初始化了tab[0]并且正在传递tab[3] All the other elements in the array [ tab[1] to tab[999] ] are auto initalized to 0 . 数组[ tab[1]tab[999] ]中的所有其他元素自动初始化为0

您仅初始化了第一个数组元素,因此其余元素将隐式地填充为0。现在,当您尝试打印第三个元素时,它将为零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM