简体   繁体   English

指向结构数组中的第一个结构?

[英]Pointer to a first structure in an array of structures?

I have a function that is supposed to be creating structures and storing them in an array of structures. 我有一个函数,该函数应该创建结构并将它们存储在结构数组中。

Lets say this is my structure: 可以说这是我的结构:

struct str {
    ...
};

The function takes a parameter that looks like this: 该函数采用如下所示的参数:

struct str **ptr

Which is a pointer to a pointer that points to a structure if I get it right and I am supposed to set this parameter to be pointing to the first element of the array of structures which will be the first structure in that array. 如果我正确理解,则它是指向结构的指针,我应该将此参数设置为指向结构数组的第一个元素,该元素将是该数组中的第一个结构。

So, if the parameter looks like this, that means that instead of having just an array of structures, I have an array of pointers that point to that structure, right? 因此,如果参数看起来像这样,则意味着我不仅拥有一个结构数组,还拥有一个指向该结构的指针数组,对吗?

Now I have declared the array of pointers to structures like this: 现在,我已经声明了指向这样的结构的指针数组:

 struct str *structures[20];

because I am expecting to have 20 (pointers to )structures store there. 因为我希望在那里存储20个(指向)结构。

Then I have a loop that is allocating memory for the structures and is storing them in the array like this: 然后我有一个循环,为结构分配内存并将其存储在数组中,如下所示:

 struct str *structure;

 structure = malloc(sizeof(struct str));

Then I fill in the parameters of the structure I want like this: 然后我像这样填写我想要的结构的参数:

structure->a = 1; structure->b = 2; .........

And I store it in the array like this: 然后将其存储在数组中,如下所示:

structures[n] = structure;

And I make sure it is stored in the array at the position where I wanted it to be by printing a parameter from the structure each loop like this: 我通过在每个循环的结构中打印一个参数,确保将其存储在数组中我想要的位置:

printf("%d\n", structures[7]->a);

My question is, how do I set the parameter of the function to be pointing to the first pointer to a structure in that array? 我的问题是,如何将函数的参数设置为指向该数组中结构的第一个指针?

And did I get it right? 我说对了吗?

Also, the parameter needs to be taken from main. 另外,该参数需要从main中获取。

And I need to work with these (pointers to)structures of the array outside this one function as well. 而且我还需要处理此功能之外的数组的这些(指向)结构。

What will the parameter look like? 参数将是什么样?

struct str *pointer; ?

Also, wouldn't it be possible to just point the pointer to the array itself and it should automatically be pointing to the first element in it? 另外,难道不能仅将指针指向数组本身,而应该自动指向数组中的第一个元素吗?

So that I could then reference the nth member of the array via the pointer since I need to work with all of them later on? 这样我就可以通过指针引用数组的第n个成员了,因为以后我需要使用所有这些成员?

I have looked at this thread, but I'm still a bit confused so I'd appreciate any feedback. 我已经看过这个主题了,但是我还是有些困惑,因此,希望得到您的反馈。

Reading through this, I notice a few inconsistencies in your post: 通过阅读此内容,我发现您的帖子中存在一些不一致之处:

Why create an array of pointers to your struct as opposed to a simple array of structs? 为什么要创建指向结构的指针数组而不是简单的结构数组? That is, let's say you have this declaration for your struct: 也就是说,假设您的结构具有以下声明:

struct myStruct {
    int x;
    int y;
};

If you want to create an array of those structs just do: 如果要创建这些结构的数组,请执行以下操作:

//dynamic
struct myStruct* myArray = malloc(20*sizeof(struct myStruct));
//static
struct myStruct* myArray[20];

Note that both of these return a pointer to the first element of the array (which is of type struct myStruct ). 请注意,这两个都返回指向数组第一个元素的指针(类型为struct myStruct )。 Your array is an array of struct myStruct elements, not an array of struct myStruct* . 您的数组是struct myStruct元素数组, 而不是 struct myStruct*数组。

An array of struct myStruct elements with size 20 is, for practical purposes, nothing more than contiguous memory big enough to hold 20 struct myStruct elements and represented by the pointer to the first element. struct myStruct ,大小为20的struct myStruct元素数组struct myStruct是足以容纳20个struct myStruct元素并由指向第一个元素的指针表示的连续内存。 Since the memory is contiguous, you can access the 'nth' element of your array by myArray[0] or *(myArray+n) (the latter is possible because an "array" is nothing more than a pointer to the first element of contiguous memory... so "adding" 1 to a pointer of a given type gives you the memory address of the next element... which you deference using the * operator to get the element itself). 由于内存是连续的,因此可以通过myArray[0]*(myArray+n)来访问数组的'nth'元素(后者是可能的,因为“数组”仅是指向数组的第一个元素的指针)连续内存...因此将“ 1”添加到给定类型的指针将为您提供下一个元素的内存地址...您可以使用*运算符将其引用以获取元素本身)。

It'll be much easier (not to mention much faster) to allocate memory for your array of structs once rather than creating an array of pointers to your structs. 为结构数组分配一次内存要比创建指向结构的指针数组容易得多(更不用说更快了)。 And then modify them in place. 然后修改它们到位。

So a function that takes in the array and increments that 5th and 6th elements might look like: 因此,一个接受数组并递增第5个和第6个元素的函数可能类似于:

void modifyStructs (struct myStruct* myArray)
{
    //modify the fifth struct:
    ++myArray[4].x;
    //modify the sixth struct:
    ++myArray[5].x;
}

The few times you'll want an array of pointers are when: 几次需要指针数组的时间是:

  1. Creating a multidimensional array. 创建多维数组。 Even then, this is really bad for cache performance... it's better to create a 1D array and define a macro for how to access the elements. 即使这样,这对于高速缓存性能还是很不利的……最好创建一个一维数组并定义一个用于访问元素的宏。 BLAS/LAPACK do this. BLAS / LAPACK为此。
  2. Creating an array where some elements should be NULL and you're not using structs. 创建一个数组,其中某些元素应为NULL,并且您不使用结构。 If you're using structs, it's much better to just define a field to identify whether your struct has been populated yet and use a simple array of structs. 如果您使用的是结构,最好定义一个字段以标识您的结构是否已填充,并使用简单的结构数组会更好。
  3. Creating an array to access a huge number of items but you don't want to allocate all that memory yet. 创建一个数组来访问大量项目,但是您还不想分配所有内存。 Here, you don't really have a choice. 在这里,您实际上别无选择。

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

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