473,473 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

10 New Member
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
Aug 24 '09 #1
19 3093
donbock
2,426 Recognized Expert Top Contributor
I suggest you start by reading Arrays Revealed.
Let us know if you have specific further questions.
Aug 24 '09 #2
msingh00
10 New Member
@donbock
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
Aug 24 '09 #3
donbock
2,426 Recognized Expert Top Contributor
@msingh00
That would be a three-dimensional array: int array[10][3][3];

@msingh00
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.
Aug 24 '09 #4
msingh00
10 New Member
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
Aug 24 '09 #5
donbock
2,426 Recognized Expert Top Contributor
Please take another look at Arrays Revealed.
I'm not kidding -- it answers your question!
Aug 24 '09 #6
msingh00
10 New Member
Thanks , I will look at it again.

--M Singh
Aug 24 '09 #7
JosAH
11,448 Recognized Expert MVP
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
Aug 25 '09 #8
msingh00
10 New Member
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
Aug 25 '09 #9
JosAH
11,448 Recognized Expert MVP
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
Aug 25 '09 #10
donbock
2,426 Recognized Expert Top Contributor
@msingh00
  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.

@msingh00
I have no idea what this means.

============
Jos beat me to the punch ... again! ;-}
Aug 25 '09 #11
msingh00
10 New Member
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
Aug 25 '09 #12
msingh00
10 New Member
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
Aug 25 '09 #13
donbock
2,426 Recognized Expert Top Contributor
@msingh00
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.
Aug 25 '09 #14
msingh00
10 New Member
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
Aug 26 '09 #15
unauthorized
81 New Member
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.
Aug 26 '09 #16
JosAH
11,448 Recognized Expert MVP
@unauthorized
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
Aug 26 '09 #17
msingh00
10 New Member
Thanks
I will try this code.
Aug 26 '09 #18
donbock
2,426 Recognized Expert Top Contributor
@JosAH
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.
Aug 26 '09 #19
JosAH
11,448 Recognized Expert MVP
@donbock
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
Aug 27 '09 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Pjotr Wedersteers | last post by:
Hello, I tried to create an array with 1000 cells, keys 0 thru 999 using $myarr = array (1000); But this leads to an array of 1 cell with value 1000; Now I have a workable solution for...
4
by: Christian Hackl | last post by:
I honestly wasn't able to find an answer for this design question using Google and Google Groups, so I apologize if it is asked too frequently :) Anyway: Let's say I have a multidimensional array...
7
by: Bob Rock | last post by:
Hello, this may seem a strange question, but is there a way of being able to call methods of a class through an array of that class when not referencing a specific object in the array. In other...
1
by: Andy Ganczak | last post by:
I want to create a data structure in which a 2x2 array contains elements that themselves are arrays and integers. array 0 1 2 ... 0 array1 array2 array3 1 array4 array5 ...
16
by: Howard Jess | last post by:
All -- I'm trying to solve a problem for which I think the solution will be to *cheat*; but I don't mind doing so for this case. The background is: Given an object constructor, and an instance...
5
by: Sadeq | last post by:
Is it possible to define custom attributes for arrays? And if so, how can I retrieve them? I mean I want to define sth like: int MyArray; and then retrieve the value of the custom...
10
by: nambissan.nisha | last post by:
I am facing this problem.... I have to define a structure at runtime as the user specifies... The user will tell the number of fields,the actual fields...(maybe basic or array types or...
13
by: John | last post by:
Is this a valid C++ program that will not crash on any machine? #include <iostream> using namespace std; int main( void ) { int i; cin >i; double X; X = 1123;
2
by: =?Utf-8?B?Z2FkeWE=?= | last post by:
I use one of 2 arrays dependent on the country. Rather than say: if exchangeID = 1 then dim myPlaceBets() as As UK.exchange.PlaceBets many statements myPlaceBetsReq.bets = myPlaceBets else...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.