C语言 为结构数组动态分配内存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19948733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:57:18  来源:igfitidea点击:

Dynamically allocate memory for Array of Structs

carrayspointersstructmalloc

提问by Mark L?we

Here's what I'm trying to do:

这是我想要做的:

#include <stdio.h>
#include <stdlib.h>

struct myStruct {
    int myVar;
}

struct myStruct myBigList = null;

void defineMyList(struct myStruct *myArray)
{
     myStruct *myArray = malloc(10 * sizeof(myStruct));

     *myArray[0] = '42';
}

int main()
{
     defineMyList(&myBigList);
}

I'm writing a simple C program to accomplish this. I'm using the GNU99 Xcode 5.0.1 compiler. I've read many examples, and the compiler seems to disagree about where to use the structtag. Using a structreference inside the sizeof()command doesn't seem to recognize the structat all.

我正在编写一个简单的 C 程序来实现这一点。我正在使用 GNU99 Xcode 5.0.1 编译器。我读过很多例子,编译器似乎不同意在哪里使用struct标签。structsizeof()命令中使用引用似乎根本无法识别struct

回答by unwind

There are a few errors in your code. Make it:

您的代码中有一些错误。做了:

struct myStruct *myBigList = NULL; /* Pointer, and upper-case NULL in C. */

/* Must accept pointer to pointer to change caller's variable. */
void defineMyList(struct myStruct **myArray)
{
     /* Avoid repeating the type name in sizeof. */
     *myArray = malloc(10 * sizeof **myArray);

     /* Access was wrong, must use member name inside structure. */
     (*myArray)[0].myVar = 42;
}

int main()
{
     defineMyList(&myBigList);
     return 0; /* added missing return */
}

Basically you must use the structkeyword unless you typedefit away, and the global variable myBigListhad the wrong type.

基本上你必须使用struct关键字,除非你typedef离开它,并且全局变量myBigList的类型错误。

回答by fkl

This is because struct name is not automatically converted into a type name. In C (not C++) you have to explicitly typedef a type name.

这是因为struct name 不会自动转换为类型 name。在 C(不是 C++)中,你必须显式 typedef 一个类型名称。

Either use

要么使用

struct myStruct instance;

when using the type name OR typedef it like this

像这样使用类型名称或 typedef 时

typedef struct {
    int myVar;
} myStruct;

now myStructcan simply be used as a type name similar to int or any other type.

现在myStruct可以简单地用作类似于 int 或任何其他类型的类型名称。

Note that this is only needed in C. C++ automatically typedefs each struct / class name.

请注意,这仅在 C 中需要。C++ 自动对每个结构/类名称进行类型定义。

A good convention when extending this to structs containing pointers to the same type is here

将其扩展到包含指向同一类型的指针的结构时,一个很好的约定是here

回答by x5lcfd

    sizeof(struct myStruct)

or

或者

    typedef struct myStruct myStrut;
    sizeof(myStruct)

回答by RazvanD

In order to work for all 10 elements for that array the line:

为了处理该数组的所有 10 个元素,该行:

myArray[0].myVar = '42';

should be:

应该:

(*myArray)[0].myVar = '42';

回答by Nirav Ambaliya

Shouldn't the following statement

不应该是下面的说法

myArray[0].myVar = '42'; 

be this?

是这个?

(*myArray)[0].myVar = 42;

myvar is an integer.

myvar 是一个整数。