简体   繁体   English

如何初始化结构内部的指向NULL的指针数组,并且该结构位于其他结构的数组中

[英]How to initialise an array of pointers to NULL that's inside a struct, and the struct is within an array of other structs

So I have a structure that has an array of pointers inside (to another struct). 所以我有一个结构,里面有一个指向另一个结构的指针数组。 I was trying to initialise that array to NULL but it seems you can't do it in C. How do you initialise the array Rpr to NULL? 我试图将数组初始化为NULL,但似乎无法在C中完成。如何将数组Rpr初始化为NULL?

I tried to do it the other way like you see in the code below, but it doesnt work. 我尝试以其他方式执行此操作,就像您在下面的代码中看到的那样,但是它不起作用。

typedef struct {maillon *Rpr [7];} repere;
repere R[26];

for(int i=0;i<26;i++){
   R[i].Rpr={NULL};
}

it pops some random insignificant errors. 它会弹出一些随机的无关紧要的错误。

C distinguishes between assignment (which is an expression) and initialization (which is part of the definition of a variable). C在赋值(这是一个表达式)和初始化(这是变量的定义的一部分)之间进行区分。 In particular, the = { ... } syntax is only valid for initialization. 特别是, = { ... }语法仅对初始化有效。

If you want to do this with a separate loop, you need two nested loops (or a lot of repeated code to set all the elements): 如果要使用一个单独的循环执行此操作,则需要两个嵌套循环(或许多重复的代码来设置所有元素):

for (int i=0; i<26; i++) {
    for (int j = 0; j<7; j++) {
        R[i].Rpr[j] = NULL;
    }
}

Alternatively, just use initialization: 或者,只需使用初始化:

repere R[26] = { { { NULL } } };

Technically this only initializes the first element of the inner array in the struct in the first element of the outer array, but C has no "half-initialized" variables. 从技术上讲,这仅在外部数组的第一个元素中的结构中初始化内部数组的第一个元素,但是C没有“半初始化”变量。 The rule is that if there's an initializer that doesn't cover all of the variable's fields, they are implicitly set to zero. 规则是,如果有一个初始化程序未覆盖变量的所有字段,则将它们隐式设置为零。 For pointers that means they're all null pointers. 对于指针,这意味着它们都是空指针。

In fact, this "initialize everything with 0" initializer can be made both shorter and more generic: 实际上,可以使“用0初始化所有内容”初始化程序变得更短,更通用:

repere R[26] = { 0 };

This { 0 } initializer works for other types as well. { 0 }初始化程序也适用于其他类型。 (Some compilers warn about this construct (" missing braces in initializer " and similar). Those compilers are stupid and need to stop.) (一些编译器对此构造发出警告(“ 初始化器中缺少大括号 ”等)。这些编译器很愚蠢,需要停止。)

You would have to do something like this: 您将必须执行以下操作:

for(int i=0;i<26;i++){
    for(int j=0; j<7; j++)
       R[i].Rpr[j]=NULL;
}

But you can skip the loop completely by writing repere R[26]={0}; 但是您可以通过写repere R[26]={0};完全跳过循环repere R[26]={0}; That will init the whole memory of the struct to zero, irregardless of how many fields there are or what type they are. 不管结构有多少个字段或类型是什么,这都会将结构的整个内存初始化为零。

一种简单的方法

   memset(R, 0, sizeof(R));

As others pointed, Here 正如其他人指出的那样

maillon *Rpr [7];

Rpr is array of pointers of maillon type and you need to initialize each pointers with NULL . Rprmaillon类型的指针数组,您需要使用NULL初始化每个指针。 For eg 例如

for (int i=0; i<26; i++) {
    for (int j = 0; j<7; j++) { /* Rpr is array of 7 pointers, so rotate 7 times */
        R[i].Rpr[j] = NULL; /* initializing each Rpr[index] with NULL */
    }
}

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

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