问题
设计
我正在尝试使用指向一个结构的指针数组,该结构中包含指向另一个与指针数组一起使用的结构的指针。
换句话说,我们必须找到一种方法来设计“一个指针数组指向一个结构,其中包含一个指向另一个指针 结构 用作数组".
我会专注于此。
指针数组
但是,我不确定如何处理指针数组的引入。
好吧,我们的代码中没有任何个指针数组……你必须先创建一个。
C 指针、数组、结构及其所有组合在互联网上遍布无数个线程,这些线程被回答了数次。我将向您展示一些基本示例,以便您可以启动并运行,只是不要指望我给您一堂指导课。
解决方案
设计
这是代码(我更改了一些变量名以使其更具可读性):
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
double value;
}Particle;
typedef struct
{
int width;
int height;
Particle *particles;
}Grid;
Grid *createGrid(int width, int height)
{
Grid *result = malloc(sizeof(Grid));
if (result == NULL)
{
printf("Error while allocating memory for the grid structure!\n");
exit(1);
}else
{
result->width = width;
result->height = height;
result->particles = malloc(sizeof(Particle) * width*height);
}
return result;
}
void destroyGrid(Grid *grid)
{
free(grid->particles);
free(grid);
}
int main()
{
Grid *grids[3];
for (int i = 0; i < 3; i++)
grids[i] = createGrid(5, 3);
/* Assign, modify, read or copy values from the array of pointers here */
for (int i = 0; i < 3; i++)
destroyGrid(grids[i]);
return 0;
}
如您所见,我介绍了 2 个函数让您的生活更轻松:createGrid 和 destroyGrid。
第一个需要您指定 2 个参数:网格的 width 和 height(代码中的 m 和 n)。然后该函数将分配内存并返回一个指针指向它刚刚创建的网格。
第二个需要你指定 1 个参数:一个 pointer 指向一个网格。然后该函数将释放第一个函数分配的所有内存,避免任何内存泄漏。
我还在主函数中添加了一个指针数组:grids。这结合上述函数的使用(都在 main 函数中),解决了第一个问题。
最终设计如下:
"我们有一个 指针数组 指向一个 struct (grids),其中包含一个 指针 指向另一个 struct (particles) 应该用作数组"。
指针数组
您现在可能想知道:“好的,那么我应该如何使用这个指向结构混乱的指针数组?”正如我之前所说,我将仅向您展示一个实际示例。
假设您要将第一个网格的第三个粒子的值设置为 2.7182:(您应该将此代码插入我注释为 /* Assign, modify, read or copy values from the array of pointers here */ 的位置)
grids[0]->particles[2].value = 2.7182;
这可以从左到右读为:“从指针数组grids,获取索引为0的元素(指向Grid结构的第一个指针)。访问其结构成员@987654336 @,它是一个指针。将该指针视为一个数组并获取索引为 2 的元素(第三个 Particle 结构)。访问其成员 value 并为其分配值 2.7182"
然后你可以做一些事情,比如将一个粒子复制到另一个粒子:
grids[1]->particles[1] = grids[0]->particles[2];
这里有一些链接可以更好地理解指针、数组和结构。实践和实验是处理它们的最佳方式:
更新
我意识到我应该做的是使用inparticles指针指向同一个网格指针中的多个粒子
我需要使用一个单独的指向粒子的指针数组。网格结构内部的数组只是指向粒子的一个子集。
目标:“使用 inparticles 指针指向多个(顺序或非顺序?)粒子的子集(在其他地方进行管理)”。
如果您想要顺序解决方案,您可以使用指针,表示指向数组的基地址(或偏移,如果您只想要一个子集)的指针(这是一个指向数组的指针)。这样做的好处是不需要你为每个指针分配额外的内存(因为只需要基地址),但它的缺点是你只能访问彼此相邻的粒子。
例子:
typedef struct
{
int width;
int height;
Particle *subparticles; //Represents a pointer to an array subset
}Grid;
/*...*/
grids[0]->subparticles = &particles[0];
grids[1]->subparticles = &particles[1];
printf("%f equals %f", grids[0]->subparticles[1].value, grids[1]->subparticles[0].value);
/*...*/
但是,如果您想要非顺序解决方案,您可以使用双指针(表示指针数组),同时必须手动为数组分配内存。这为您提供了更大的灵活性,但代价是内存分配开销。
typedef struct
{
/*...other members...*/
Particle **subparticles;
}Grid;
Grid *createGrid(int subparticlesCount)
{
Grid *result = malloc(sizeof(Grid));
if (result == NULL)
{
printf("Error while allocating memory for the grid structure!\n");
exit(1);
}else
{
result->subparticles = malloc(sizeof(Particle *) * subparticlesCount);
}
return result;
}
/*...*/
grids[0]->subparticles[0] = &particles[1];
grids[0]->subparticles[1] = &particles[5];
grids[0]->subparticles[2] = &particles[3];
grids[1]->subparticles[0] = &particles[3];
printf("%f equals %f", grids[0]->subparticles[2]->value, grids[1]->subparticles[0]->value);
/*...*/