-2

I`m curious about if there are cases to use a 2D array of structures, like f.e.:

typedef struct
{
 //member variables
}myStruct;

myStruct array2D[x][y]; 
//Uses cases of array2D[x][y]? Do we need them?

Why should you use a 2D array of structs?

Leon
  • 346
  • 3
  • 15
  • Is the reason why the question is being downvoted because it's too generic in a sense? – Leon Feb 02 '20 at 09:30
  • 1
    `struct RGB {float R; float G; float b}; struct RGB display [1920][1080];` – hetepeperfan Feb 02 '20 at 09:34
  • @Leon It is probably downvoted because the question shows less background information, research effort and because of "should" question are commonly deprecated amongst the community. Unfortunately, I experienced this by myself often here. Don´t take it personal. I think it is a good question to ask for. – RobertS supports Monica Cellio Feb 02 '20 at 09:46
  • @RobertSsupportsMonicaCellio Ahh, I see, it's one of those things – Leon Feb 02 '20 at 20:38

2 Answers2

1

Why should you use a 2D array of structs?

A defined structure is basically just a type like any other (though of course, any type is different from the other and there is a difference between private and standard datatypes; not even mentioning the differences in memory allocation between objects of that types) with which you can declare objects.

You could also ask: Why should you use a char,int or double two-dimensional array?

It is not only a structure own kind of thing.

It depends on the context and its worth to have a clear "structure" in ones code; So to code in this way can help you to make your code more readable and clear, if you need a huge amount of objects of a certain type, in this case a structure.

Maybe you even want to group some objects and/or want to treat them differently. In this case, multiple array dimensions are beneficial because you can address objects of each dimension explicitly and separately.

As @buysbee mentioned in the comments:

One obvious example, where it is beneficial to store structure objects in a two-dimensional array is to storing the pixels of a picture. It is better to store the "pixel" structure objects in a two-dimensional array because this emulates how a picture is constructed of naturally.

  • Hmm,so based on your answer, you use a 2D array of structs when you need one(ie depends on context)? – Leon Feb 02 '20 at 09:29
  • 1
    You might like to add one obvious use case: storing images. Each pixel is stored in a `struct` and all pixels of the image are stored in a 2D array. Most images need some more meta information, so this array will probably be an element in another `struct`. – the busybee Feb 02 '20 at 09:30
  • @Leon No. I would do so, if I see a benefit in separating them per dimension. This could be to treat them differently or just to increase readability by dividing them into several dimensions if I have a huge amount of objects of that type. – RobertS supports Monica Cellio Feb 02 '20 at 09:34
  • @thebusybee Thank you very much for the good example. I took it into the answer. – RobertS supports Monica Cellio Feb 02 '20 at 10:14
0

We can add more questions: why we should use 3D, 4D. 5D ...... 10000000D arrays of something.

Data structures used in a project depend on the problem to be solved and the algorithm chosen.

0___________
  • 60,014
  • 4
  • 34
  • 74