简体   繁体   English

创建一个动态的结构体数组

[英]Creating a dynamic array of structs

I need to create an array of structs but I have to read the array size from a file. 我需要创建一个结构数组,但必须从文件中读取数组大小。 My problem is that I might be using the pointers all wrong. 我的问题是我可能使用所有错误的指针。 I can't use global variables. 我不能使用全局变量。 Here's the code I made : 这是我编写的代码:

#include <stdio.h>
#include <stdlib.h>
struct driver
{
       int *amount;
       int ADT1[9];
       int ADO1[9];
};

struct driver totaldrivers[*amount];

The errors(2) I get are "amount undeclared here(not in a function)" and "storage size of 'totaldrivers' isn't known". 我得到的errors(2)是“此处未声明的量(不在函数中)”和“'totaldrivers'的存储大小未知”。 I tried giving the *amount a value directly but no luck and even so the amount must be taken by the file the user has created and the number is the first line. 我尝试直接给* amount一个值,但是没有运气,即使如此,该量也必须由用户创建的文件占用,并且数字是第一行。 And that's where I have my issue. 这就是我的问题所在。 The (.txt)file format is: (.txt)文件格式为:

4(amount of entries) 4(参赛作品)
123456789 , 978675645 (ADT1,ADO1) 123456789,978675645(ADT1,ADO1)
.
.
.
I am sure I've made rookie mistakes so forgive me if the question is silly. 我敢肯定我犯了新秀错误,所以如果这个问题很傻,请原谅我。

I'm assuming the contents of your structure should be as follows (I've removed int *amount ): 我假设您的结构内容应如下所示(我删除了int *amount ):

struct driver
{
    int ADT1[9];
    int ADO1[9];
};

To create a dynamic 1D array of these structures, first create a pointer to a 1D array of driver : 要创建这些结构的动态一维数组,请首先创建一个指向driver一维数组的指针:

struct driver *pDriver;

Next, allocate memory for the number of elements you need in the array. 接下来,为数组中所需的元素数量分配内存。 This should be known ( N ): 这应该是已知的( N ):

pDriver = malloc(N * sizeof(*pDriver));

If you want to keep a record of the number of elements, you should do something like this: 如果要保留元素数量的记录,则应执行以下操作:

struct driver
{
    int ADT1[9];
    int ADO1[9];
};

struct driverArray
{
    int numDrivers;
    struct driver *pDriver;
};

struct driverArray driverArr;

driverArr.numDrivers = N;
driverArr.pDriver = malloc(driverArr.numDrivers * sizeof(*driverArr.pDriver));

Rather than 而不是

struct driver totaldrivers[*amount];

Set up a pointer to refer to it, then allocate the array from the heap when you know the size you'll need. 设置一个引用它的指针,然后在知道所需大小时从堆中分配数组。

struct driver totaldrivers[];
int amount=7; /* for example */
totaldrivers=malloc(sizeof(struct driver)*amount);
/* Apology for the typo; I wrote "driver=" the first time. Good catch! */

You also probably don't want that "int *amount" inside the struct. 您也可能不希望结构中包含“ int * amount”。

struct driver totaldrivers[*amount]; 

here, you use array (first method): 在这里,您使用数组 (第一种方法):

* amount is the size the array of driver struct. * amount是驱动程序结构体数组的大小。 this is a wrong way to define size of array !! 这是定义数组大小的错误方法! there is many way to define the size of array some of them: 有很多方法来定义数组的大小,其中一些:

1) preprocessor using define directive: #define size 12 1)使用define指令的预处理器: #define size 12

2) global variable : int size 12 2)全局变量:int size 12

you can also use pointer (second method): 您还可以使用指针 (第二种方法):

struct driver *totaldrivers;

totaldrivers= malloc(nb * sizeof(*totaldrivers)); // nb : number of elements  

you should not complicated your code take it easy ! 您不应该使代码复杂化就容易了!

Another way of doing this is to use the dynamic stack allocation facility introduced in C99. 执行此操作的另一种方法是使用C99中引入的动态堆栈分配工具。 Just make sure your compiler is C99 compilant. 只要确保您的编译器是C99编译器即可。

As the others have said, dispense with amount. 正如其他人所说,省去数量。

struct driver
{
    int ADT1[9];
    int ADO1[9];
};

Introduce a function to do whatever you want. 介绍一个函数来执行您想要的任何事情。 Allocate the array here 在这里分配数组

void RunWith(int amount)
{
    struct driver totaldrivers[amount];

    ... 
}

In the caller, pass in the amount 在来电者中,转入金额

int main ()
{
    RunWith(25);
    ...
    return 0;
}

There is no need to malloc or free. 不需要malloc或free。

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

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