Pointer to an array of 2D structure C

I have a specific structX structure and a 2D array that contains such structures.

I want to keep a pointer to this 2D structure and iterate over it

in a dynamic way, that is, a pointer can contain any structX and iterations.

An example in common 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 iteration:

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

I hope I understand, because I said that this is very common.

How can you do an iteration? What does the absence of errors mean?

Thanks!

+4
source share
2 answers

Two problems that I can notice in your code if(ptr[x][y] == OK)

(1):

ptr is a pointer to a structure (single *), you cannot use double indices [][] , so the error is in if(ptr[x][y] == OK)

error, indexed value is neither an array nor a pointer due to ptr[][]

(2):

error: a structure type value is used where a scalar is required means if(struct are not allow) .

 if(should be a scalar value ) 

The value of the scalar value can be converted to 0/1.

Pointer to an array of 2D structure C

 struct structX matrix2D[ROW][COL]; 

pointer

 struct structX (*ptr2D)[ROW][COL]; ptr2D = &matrix2D; 

ok, access the array structure as follows:

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

If you want to pass a function, follow these steps:

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

Just to make sure that I wrote a simple code, it shows how to work with 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; ia = 5; ib = '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 his work, output:

 5 a in to: 5 a 

EDIT

I wanted to show two tricks, but now I think that I should provide a single 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; ia = 5; ib = '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: a structure type value is used where a scalar is required means if(struct are not allow) .

 if(should be a scalar value ) 

you cannot do like if((*ptr2D)[r][c]);

but it allows you to:

 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); 
+2
source

You might want to prepare this article on multidimensional arrays. If you want to iterate over an array, you need to know how big it is (dynamic or not). If you want it to be dynamic, it means that you need to allocate memory for it when it needs to grow, and you need to free up the old memory. You also have a problem in your question - you declare a single pointer that is null and then try to dereference it, but you never allocated memory to it.

If you have allocated a memory for this, you can play it by saying

 ptr[x * ROW_WIDTH + y] 

if you set ROW_WIDTH to the maximum value of y. Depending on whether you want to represent an array of rows or a main array of columns, you can use the width y * instead of the width x *.

+2
source

All Articles