Connecting Tech Pros Worldwide Forums | Help | Site Map

Defining a 1-D array of 2-D Arrays in C

Newbie
 
Join Date: Aug 2009
Posts: 10
#1: Aug 24 '09
Hello Friends,

I want to declare a 1-D array which will contain its elements as a 2-D array of integers, in C. Here, I want to access the 2-D arrays as a whole.
I tried searching the texts and the net too, but could not find/understand something significant.

Can someone help please.??

Thanks
--M Singh

Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 831
#2: Aug 24 '09

re: Defining a 1-D array of 2-D Arrays in C


I suggest you start by reading Arrays Revealed.
Let us know if you have specific further questions.
Newbie
 
Join Date: Aug 2009
Posts: 10
#3: Aug 24 '09

re: Defining a 1-D array of 2-D Arrays in C


Quote:

Originally Posted by donbock View Post

I suggest you start by reading Arrays Revealed.
Let us know if you have specific further questions.

Thanks for the response. However, this has not solved my problem.
Actually, I want something as following:-

I have to store 2-D arrays like:- [1 2 3; 4 5 6; 7 8 9] [2 3 4; 5 6 7; 8 9 10].... and so on (say 10 arrays lke this) . Now I want to store these arrays in another array like [ [....] [...] [...] [...] ...[...] ].

So, can I do this in C, if yes, then how do I declare the array of arrays.

Please help.

Thanks
---M Singh
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 831
#4: Aug 24 '09

re: Defining a 1-D array of 2-D Arrays in C


Quote:

Originally Posted by msingh00 View Post

I have to store 2-D arrays like:- [1 2 3; 4 5 6; 7 8 9] [2 3 4; 5 6 7; 8 9 10].... and so on (say 10 arrays lke this) .

That would be a three-dimensional array: int array[10][3][3];

Quote:

Originally Posted by msingh00 View Post

Now I want to store these arrays in another array like [ [....] [...] [...] [...] ...[...] ].

That would be a four-dimensional array: int array[6][10][3][3];
(if, for example, you wanted to store six of "these arrays in another array".)

By the way, the correct initializer for your first example would be
Expand|Select|Wrap|Line Numbers
  1. int array[10][3][3] = {
  2.     { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} },
  3.     { {2, 3, 4}, {5, 6, 7}, {8, 9, 10} },
  4.     ...
  5. };
So far I've assumed that you want a static allocation for the arrays. Refer to the article I originally cited if you want to dynamically allocate these arrays.
Newbie
 
Join Date: Aug 2009
Posts: 10
#5: Aug 24 '09

re: Defining a 1-D array of 2-D Arrays in C


Thanks, but I am still not able to make my point clear to you.

Actually, I want to store the 2-Darrays say, A1,A2,A3,...,A10 in a 1-D array say, B, such that when I say B[0] then I get the whole array A1, for B[1] I get the whole array A2 and so on...

Is it possible to do so in 'C'? If, yes, then how ?

I think I am more clear now.

Thanks,
--M Singh
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 831
#6: Aug 24 '09

re: Defining a 1-D array of 2-D Arrays in C


Please take another look at Arrays Revealed.
I'm not kidding -- it answers your question!
Newbie
 
Join Date: Aug 2009
Posts: 10
#7: Aug 24 '09

re: Defining a 1-D array of 2-D Arrays in C


Thanks , I will look at it again.

--M Singh
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#8: Aug 25 '09

re: Defining a 1-D array of 2-D Arrays in C


Also have a look at the folowing 'trick':

Expand|Select|Wrap|Line Numbers
  1. typedef struct { int[3][3] m; } matrix;
  2.  
The identifier 'matrix' defines a struct that contains a square matrix, so the following:

Expand|Select|Wrap|Line Numbers
  1. matrix list[10];
  2.  
... defines ten elements of type 'matrix'. Structs are copied by value so the entire matrix element 'm' will be copied, e.g.

Expand|Select|Wrap|Line Numbers
  1. matrix f(matrix mat) {
  2.    for (int i= 0; i < 3; i++) mat.m[i][i]= 0;
  3.    return mat;
  4. {
  5.  
This function returns an entire matrix with its diagonal set to zero.

kind regards,

Jos
Newbie
 
Join Date: Aug 2009
Posts: 10
#9: Aug 25 '09

re: Defining a 1-D array of 2-D Arrays in C


Dear Mr/Mrs Jos

Thanks for your reply. However the following code similar to the one that you suggested, does not compile and the error message is as mentioend against the respective lines below:-


typedef struct {int m[3][3]; } matrix;
matrix list[10];

matrix f(matrix mat[10])
{
int i;
for(i=0;i<3;i++)
mat.m[i][i]=i; // " Structure required on left side of . or .* "
return(mat); // "Incompatible type conversion:
}


main()
{
matrix *here;
f(list);
}


Also, I want to access/return the array element "m" of the array of structures "list" and "not the individual elements of the inner array m".

Please guide me.

Thanks and Regards
--M Singh
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#10: Aug 25 '09

re: Defining a 1-D array of 2-D Arrays in C


You're passing an entire array of matrixes; either change this:

Expand|Select|Wrap|Line Numbers
  1. matrix f(matrix mat[10])
  2.  
... to this:

Expand|Select|Wrap|Line Numbers
  1. matrix f(matrix mat)
  2.  
... to pass a single matrix or change the body of your function to something like this:

Expand|Select|Wrap|Line Numbers
  1. int i;
  2. int j= 0; // e.g. change the zeroth matrix
  3. for(i=0;i<3;i++)
  4. mat[j].m[i][i]=i; 
  5. return(mat[j]);
  6. }
  7.  
kind regards,

Jos
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 831
#11: Aug 25 '09

re: Defining a 1-D array of 2-D Arrays in C


Quote:

Originally Posted by msingh00 View Post

Expand|Select|Wrap|Line Numbers
  1. typedef struct {int m[3][3]; } matrix;
  2. matrix list[10];
  3.  
  4. matrix f(matrix mat[10]) {
  5.   int i;
  6.   for(i=0;i<3;i++)
  7.     mat.m[i][i]=i;        // "Structure required on left side of . or .* "
  8.   return(mat);            // "Incompatible type conversion:
  9. }
  10.  
  11. main() {
  12.    matrix *here;
  13.    f(list); 
  14. }

  1. Line 4 tries to declare argument mat as an array of 10 matrix structures. However, the "10" is spurious. C doesn't recognize your intent. Instead, argument mat is a pointer to a single matrix structure. Perhaps that structure is the first of an array, but the function is not aware of that. If you want to pass an array, then you should add an 'nelem' argument to inform the function how many elements are in the array.
  2. If you intend for mat to be an array of structures, then which structure in the array do you intend to access on line 7? Your failure to specify an array index is the reason for the error code.
  3. Function f returns a single matrix structure, yet on line 8 you try to return mat -- an array of matrix structures. This is the incompatibility that is described in the error message. Which of the ten matrix structures in mat is the one you want to return?
  4. Line 12 accomplishes nothing.
  5. By the way, function main should return an int -- the C Standard mandates it.
  6. Use CODE tags please.

Quote:

Originally Posted by msingh00 View Post

Also, I want to access/return the array element "m" of the array of structures "list" and "not the individual elements of the inner array m".

I have no idea what this means.

============
Jos beat me to the punch ... again! ;-}
Newbie
 
Join Date: Aug 2009
Posts: 10
#12: Aug 25 '09

re: Defining a 1-D array of 2-D Arrays in C


Thank you Mr./Mrs Jos

Yes it compiles if I make the changes suggested by you with a small variation, that is, instead of declaring "matrix f(matrix mat)" if I declare
"matrix f(matrix *mat)", then it works. So that part is ok.

But I see 1 more doubt that is as follows:-

You are asking me to declare the function as " matrix f(matrix *m) ", that means the return type is matrix (which is a structure) . whereas the actual return value mat[j] is the member of that structure.
So, how should be the declaration of the function , so that this mismatch is resolved.

Regards,
--M Singh
Newbie
 
Join Date: Aug 2009
Posts: 10
#13: Aug 25 '09

re: Defining a 1-D array of 2-D Arrays in C


Thanks Mr./Mrs. Donbock for your response too :)

However, Mr./Mrs. Jos has got my point and I have almost got my answers too :)

Regards,
--M Singh
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 831
#14: Aug 25 '09

re: Defining a 1-D array of 2-D Arrays in C


Quote:

Originally Posted by msingh00 View Post

You are asking me to declare the function as " matrix f(matrix *m) ", that means the return type is matrix (which is a structure) . whereas the actual return value mat[j] is the member of that structure.
So, how should be the declaration of the function , so that this mismatch is resolved.

What is your goal?
  • Do you want one invocation of function f to work with a single 3x3 matrix or do you want it to work with several 3x3 matrices in the array at the same time?
  • Is the purpose of function f to alter the input matrix or is it important for the caller to have the option of leaving the input unchanged and returning the output matrix separately?
It is difficult to give you advice when it isn't clear what you're trying to accomplish.
Newbie
 
Join Date: Aug 2009
Posts: 10
#15: Aug 26 '09

re: Defining a 1-D array of 2-D Arrays in C


Mr./Mrs. Donbock,

In one sentence I want to say that, :--

I want to implement a STACK which will have square matrices as its elements , INSTEAD of a individual numbers, or a characters, etc.. as its elements.
However, the elments of the square matrices are individual numbers.

So, is it possible to do so in 'C' ??

I think, I am able to convey you my requirement now.


Thanks and regards,
-- M Singh
Member
 
Join Date: May 2009
Posts: 81
#16: Aug 26 '09

re: Defining a 1-D array of 2-D Arrays in C


So basically, you want to do something like this:

Expand|Select|Wrap|Line Numbers
  1. #define MATRIX_SIZE_X 3 // let's try a 3x3 matrix
  2. #define MATRIX_SIZE_Y 3
  3. const int stack_size = 100;
  4.  
  5. typedef double[MATRIX_SIZE_X][MATRIX_SIZE_y] Matrix;
  6.  
  7. typedef struct {
  8.      Matrix[stack_size] data;
  9.      int stack_pos = 0;
  10. } Stack;
  11.  
  12. void push(Stack& s, Matrix& m)
  13. {
  14.     // todo: check array bounds!
  15.     memcpy(&s.data[s.stack_pos++], &m, sizeof(Matrix));
  16. }
  17.  
  18. Matrix pop(Stack& s)
  19. {
  20.      // todo: bound checking
  21.      return  s.data[--(s.stack_pos)];
  22. }
Mind, you this is vastly oversimplified and untested example and you will have to modify it, especially if you want to allocate the stack size during runtime.

Since C does not support the notion of classes, you have no choice but to implement all data as "individual numbers", but typedefing can make your life much easier.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#17: Aug 26 '09

re: Defining a 1-D array of 2-D Arrays in C


Quote:

Originally Posted by unauthorized View Post

Expand|Select|Wrap|Line Numbers
  1. void push(Stack& s, Matrix& m)
  2. {
  3.     // todo: check array bounds!
  4.     memcpy(&s.data[s.stack_pos++], &m, sizeof(Matrix));
  5. }
  6.  
Since C does not support the notion of classes, you have no choice but to implement all data as "individual numbers", but typedefing can make your life much easier.

C doesn't support references either; why not use my trick? Embedding a fixed size array in a struct enables you to copy the entire thing instead of just a pointer (or reference) to the real matrix. The language itself does the nitty gritty work then and there is no need to resort to memcpy().

kind regards,

Jos
Newbie
 
Join Date: Aug 2009
Posts: 10
#18: Aug 26 '09

re: Defining a 1-D array of 2-D Arrays in C


Thanks
I will try this code.
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 831
#19: Aug 26 '09

re: Defining a 1-D array of 2-D Arrays in C


Quote:

Originally Posted by JosAH View Post

... why not use my trick? Embedding a fixed size array in a struct enables you to copy the entire thing instead of just a pointer (or reference) to the real matrix. The language itself does the nitty gritty work then and there is no need to resort to memcpy().

A word of warning -- I once encountered a C compiler that implemented functions that return structures such that these functions were irretrievably non-reentrant and thread-unsafe. That limitation is probably not relevant to the OP.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#20: Aug 27 '09

re: Defining a 1-D array of 2-D Arrays in C


Quote:

Originally Posted by donbock View Post

A word of warning -- I once encountered a C compiler that implemented functions that return structures such that these functions were irretrievably non-reentrant and thread-unsafe. That limitation is probably not relevant to the OP.

That was probably a very very old C compiler. K&R1 C couldn't pass and return entire structs; you had to pass pointers instead. Before K&R2 saw the light passing structures itself was a non standard feature and some implementations were hiding the fact that they still passed pointers to them and used a local buffer and pointers to that buffer; it was really a kludge.

kind regards,

Jos
Reply