简体   繁体   English

指向2D结构数组C的指针

[英]Pointer to 2D struct array C

I have a certain struct structX and a 2D array which holds these kind of structs. 我有一个struct structX和一个包含这些结构的2D数组。

I want to be able to save a pointer to that 2D struct and iterate over it 我希望能够保存指向该2D结构的指针并迭代它

in a dynamic way, meaning, the pointer can hold any structX and iterate. 以动态的方式,意思是,指针可以包含任何structX并迭代。

Example in general lines: 一般行示例:

struct structX *ptr = NULL;

...

  if(i == OK)
    {
        ptr = General_struct_which_holds_others->ptr1;
    }
    else if(i ==NOT_OK)
    {
        ptr = General_struct_which_holds_others->ptr2;
    }

Now the iteration: 现在迭代:

if(ptr[x][y] == OK) <----Error, subscripted value is neither array nor pointer
{
...
}

I hope i'm understood, As i was saying this is very general. 我希望我明白,正如我所说,这是非常笼统的。

How can the iteration be made? 如何进行迭代? meaning not getting errors? 意思是没有错误?

Thanks! 谢谢!

Two problem I can noticce in your code if(ptr[x][y] == OK) if(ptr[x][y] == OK)我可以在你的代码中记下两个问题

(1): (1):

ptr is pointer to structure (single *) you can't use double indices [][] so error at if(ptr[x][y] == OK) ptr是指向结构的指针(单*)你不能使用双索引[][]所以错误在if(ptr[x][y] == OK)

error, subscripted value is neither array nor pointer because of ptr[][] 错误,由于ptr[][] 下标值既不是数组也不是指针

(2): (2):

error: used struct type value where scalar is required means if(struct are not allow) . error:使用了struct type value,其中标量是必需的, 表示 if(struct are not allow)

if(should be a scalar value )

scalar value means can be convert into 0/1. 标量值表示可以转换为0/1.

Pointer to 2D struct array C 指向2D结构数组C的指针

struct structX matrix2D[ROW][COL];

its pointer 它的指针

struct structX (*ptr2D)[ROW][COL];

ptr2D = &matrix2D;

ok, access you array structure like this: 好的,访问你这样的数组结构:

struct structX i;
(*ptr2D)[r][c] = i;

If you want to pass in an function do like: 如果你想传入一个函数,请执行以下操作:

void to(struct structX* ptr2D[][COL]){
   struct structX i;
   ptr2D[][COL] = i;
}
void from(){
  struct structX matrix2D[ROW][COL];
  to(matrix2D);
}

Just to make you sure I written a simple code shows how to work with ptr2D . 只是为了让你确定我写了一个简单的代码,展示了如何使用ptr2D Hope you find it helpful: 希望你觉得它有用:

#include<stdio.h>
#define ROW 10
#define COL 5
typedef struct {
 int a;
 char b;
} structX;
void to(structX ptr2D[][COL], int r, int c){
 printf("in to: %d %c\n", ptr2D[r][c].a, ptr2D[r][c].b);
}
int main(){
 structX matrix[ROW][COL];
 structX (*ptr2D)[ROW][COL];
 ptr2D = &matrix;
 structX  i;
 i.a = 5; 
 i.b = 'a';
 int r = 3;
 int c = 2;
 (*ptr2D)[r][c] = i;
 printf("%d %c\n", (*ptr2D)[r][c].a, (*ptr2D)[r][c].b);
 to(matrix, r, c);
}

And its working, Output: 它的工作,输出:

5 a
in to: 5 a

EDIT 编辑

I wanted to show two tricks but now I think I should provide a uniform method( as you commented ): So here is the code: 我想展示两个技巧,但现在我想我应该提供一个统一的方法( 正如你评论的那样 ):所以这里是代码:

#include<stdio.h>
#define ROW 10
#define COL 5
typedef struct {
 int a;
 char b;
} structX;
void to(structX (*ptr2D)[ROW][COL], int r, int c){
 printf("in to: %d %c\n", (*ptr2D)[r][c].a, (*ptr2D)[r][c].b);
}
int main(){
 structX matrix[ROW][COL];
 structX (*ptr2D)[ROW][COL];
 ptr2D = &matrix;
 structX  i;
 i.a = 5; 
 i.b = 'a';
 int r = 3;
 int c = 2;
 (*ptr2D)[r][c] = i;
 printf("%d %c\n", (*ptr2D)[r][c].a, (*ptr2D)[r][c].b);
 to(&matrix, r, c);
}

Output 产量

5 a
in to: 5 a  

EDIT : 编辑

error: used struct type value where scalar is required means if(struct are not allow) . error:使用了struct type value,其中标量是必需的, 表示 if(struct are not allow)

if(should be a scalar value )

you can't do like if((*ptr2D)[r][c]); 你不能这样做if((*ptr2D)[r][c]);

but this is allow: 但这是允许的:

if((*ptr2D)[r][c].a == 5); 

or 要么

if((*ptr2D)[r][c].b == 'a');  

or 要么

if((*ptr2D)[r][c].a == 5 && (*ptr2D)[r][c].b == 'a');  

or 要么

structX  i;
if((*ptr2D)[r][c] == i);

You might want to ready this article about multidimensional arrays. 您可能想要准备这篇关于多维数组的文章 If you want to iterate over an array, you need to know how big it is (whether it is dynamic or not). 如果你想迭代一个数组,你需要知道它有多大(无论它是否是动态的)。 If you want it to be dynamic, that means you need to allocate memory for it when it needs to grow and you need to free the old memory. 如果您希望它是动态的,那意味着您需要在需要增长时为其分配内存,并且需要释放旧内存。 You also have a problem in your question - you declare a single pointer which is null and then try to dereference it but you never allocated memory for it. 您的问题也有问题 - 您声明一个指针为null,然后尝试取消引用它,但您从未为其分配内存。

If you did allocate memory for it, you could dereference it by saying 如果你确实为它分配了内存,你可以通过说出来取消引用它

ptr[x * ROW_WIDTH + y]

if you set ROW_WIDTH to the maximum value of y. 如果将ROW_WIDTH设置为y的最大值。 Depending on whether you want to represent a rows major or column major array, you might use y * width instead of x * width. 根据您是要表示行主列还是列主数组,可以使用y * width而不是x * width。

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

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