Defining a 1-D array of 2-D Arrays in C | Newbie | | Join Date: Aug 2009
Posts: 10
| | |
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
| | | 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
| | | re: Defining a 1-D array of 2-D Arrays in C Quote:
Originally Posted by donbock 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
| | | re: Defining a 1-D array of 2-D Arrays in C Quote:
Originally Posted by msingh00 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 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 - int array[10][3][3] = {
-
{ {1, 2, 3}, {4, 5, 6}, {7, 8, 9} },
-
{ {2, 3, 4}, {5, 6, 7}, {8, 9, 10} },
-
...
-
};
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
| | | 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
| | | 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
| | | re: Defining a 1-D array of 2-D Arrays in C
Thanks , I will look at it again.
--M Singh
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: Defining a 1-D array of 2-D Arrays in C
Also have a look at the folowing 'trick': -
typedef struct { int[3][3] m; } matrix;
-
The identifier 'matrix' defines a struct that contains a square matrix, so the following:
... defines ten elements of type 'matrix'. Structs are copied by value so the entire matrix element 'm' will be copied, e.g. -
matrix f(matrix mat) {
-
for (int i= 0; i < 3; i++) mat.m[i][i]= 0;
-
return mat;
-
{
-
This function returns an entire matrix with its diagonal set to zero.
kind regards,
Jos
| | Newbie | | Join Date: Aug 2009
Posts: 10
| | | 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
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: Defining a 1-D array of 2-D Arrays in C
You're passing an entire array of matrixes; either change this:
... to this:
... to pass a single matrix or change the body of your function to something like this: -
int i;
-
int j= 0; // e.g. change the zeroth matrix
-
for(i=0;i<3;i++)
-
mat[j].m[i][i]=i;
-
return(mat[j]);
-
}
-
kind regards,
Jos
| | Expert | | Join Date: Mar 2008 Location: Naperville, Illinois U.S.
Posts: 831
| | | re: Defining a 1-D array of 2-D Arrays in C Quote:
Originally Posted by msingh00 - 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);
-
}
- 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.
- 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.
- 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?
- Line 12 accomplishes nothing.
- By the way, function main should return an int -- the C Standard mandates it.
- Use CODE tags please.
Quote:
Originally Posted by msingh00 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
| | | 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
| | | 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
| | | re: Defining a 1-D array of 2-D Arrays in C Quote:
Originally Posted by msingh00 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
| | | 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
| | | re: Defining a 1-D array of 2-D Arrays in C
So basically, you want to do something like this: - #define MATRIX_SIZE_X 3 // let's try a 3x3 matrix
-
#define MATRIX_SIZE_Y 3
-
const int stack_size = 100;
-
-
typedef double[MATRIX_SIZE_X][MATRIX_SIZE_y] Matrix;
-
-
typedef struct {
-
Matrix[stack_size] data;
-
int stack_pos = 0;
-
} Stack;
-
-
void push(Stack& s, Matrix& m)
-
{
-
// todo: check array bounds!
-
memcpy(&s.data[s.stack_pos++], &m, sizeof(Matrix));
-
}
-
-
Matrix pop(Stack& s)
-
{
-
// todo: bound checking
-
return s.data[--(s.stack_pos)];
-
}
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.
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: Defining a 1-D array of 2-D Arrays in C Quote:
Originally Posted by unauthorized - void push(Stack& s, Matrix& m)
-
{
-
// todo: check array bounds!
-
memcpy(&s.data[s.stack_pos++], &m, sizeof(Matrix));
-
}
-
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
| | | 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
| | | re: Defining a 1-D array of 2-D Arrays in C Quote:
Originally Posted by JosAH ... 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.
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: Defining a 1-D array of 2-D Arrays in C Quote:
Originally Posted by donbock 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
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,467 network members.
|