简体   繁体   English

如何传递/访问结构的双指针?

[英]How to pass/access a double pointer to/from a struct?

A 10x10 matrix is created as follows: 如下创建10x10矩阵:

double **c = (double **)pvPortMalloc(ROW * sizeof(double*));
for (i = 0; i < ROW; i++)
    c[i] = (double *)pvPortMalloc(COL * sizeof(double));

I would like to pass double **c to a struct : 我想将double **c传递给struct

struct AMessage {
    char cMessageID;
    double (**doublePtr);
} xMessage;

At the end I want to access the struct and print the matrix on a screen, however, I am unsure how I should go about this using double pointers... 最后,我想访问结构并在屏幕上打印矩阵,但是,我不确定如何使用双指针进行此操作...

Firstly, in direct answer to your question, if you define a matrix in that way and want to store its pointer in a struct, you can do it via a direct assignent. 首先,在直接回答您的问题时,如果您以这种方式定义矩阵并将其指针存储在结构中,则可以通过直接分配器来实现。 Carrying on from your example, you could make the doublePointer member of a struct AMessage point to the memory you've allocated at c simply via something like this: 从您的示例继续,您可以使struct AMessagedoublePointer成员仅通过struct AMessage指向您在c分配的内存:

struct AMessage am;
am.doublePtr = c;

But if you're wanting to create a matrix, where every row is guaranteed to have the same number of elements as any other row a better fit for representing this is a 2D array, rather than an array of pointers to other arrays. 但是,如果您要创建一个矩阵,则可以确保每行与其他任何行具有相同数量的元素,则更适合表示该矩阵的是2D数组,而不是指向其他数组的指针数组。

A 2D array in C can be declared like so: C语言中的2D数组可以这样声明:

double matrix[ROW][COL];

this declares a variable called matrix , with ROW rows and COL columns. 这将声明一个名为matrix的变量,其中包含ROW行和COL列。

There is nothing to stop you including this definition straight in the definition of your struct , making the definition of your struct like this: 没有什么可以阻止您在struct定义中直接包含此定义,使struct的定义像这样:

struct AMessage
{
     char MessageId;
     double matrix[ROW][COL];
};

(Thanks to chqrlie for suggesting this) (感谢chqrlie提出这个建议)

If you definitely want a pointer to a 2D array to be in your struct (for example, you might want to have one large array referenced by two of your struct s) you need to know that a 2D array in C is really a 1D array, only addressed in a certain way. 如果您肯定要在struct包含指向2D数组的指针(例如,您可能希望有一个大数组被两个struct引用),则需要知道C中的2D数组实际上是1D数组,仅以某种方式解决。

You can dynamically allocate the memory to be used for the matrix with one call to malloc: 您可以通过一次调用malloc动态分配要用于矩阵的内存:

double *c = malloc(ROW * COL * sizeof(*c));

One option for accessing element the element in column j , row i is: 访问元素j列,第i行中的元素的一种选择是:

double a = c[ROW * j + i];

but this is obviously a bit clunky, really, it is more convenient to access element i , j by something like this: 但这显然有点笨拙,实际上,通过以下方式访问元素ij更方便:

double a = d[j][i];

To define the pointer d that we can dereference like this we need to think of what type d[j] is. 为了定义可以像这样取消引用的指针d ,我们需要考虑d[j]是什么类型。 It is not, as many think a double * , rather it is a pointer to an array of ROW double s. 正如很多人认为的那样,它不是double * ,而是指向ROW double数组的指针。 In C syntax you would declare d like so: 在C语法中,您将像这样声明d

double (*d)[ROW];

Finally resolving your question, I would define your struct like this: 最后解决您的问题,我将像这样定义您的结构:

struct AMessage 
{
     char MessageId;
     double (*matrix)[ROW];
}

Assigning to this struct would look something like this: 分配给该struct看起来像这样:

struct AMessage am;
am.matrix = malloc(ROW * COL * sizeof(*am.matrix));
am.matrix[j][i] = 42; // Assigns 42 to row j, column i 

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

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