myRainfallDB is a three-dimensional array containing information about the rainfall in random places on earth. I want to set up the following array structure:
myRainfallDB[] contains a list of place records. These coordinates must be stored in doubles.
myRainfallDB[][] contains
- At index 0: a double containing the X coordinate of the place
- At index 1: a double containing the Y coordinate of the place
- At index 2: an array containing twelve doubles storing the amount of rainfall every month
I know I could probably get things done more efficiently by using classes, but I’m just learning C so I only want to toy with arrays for now. How would I go about declaring this 3D array?
You need a structure or simply put a user-defined data structure. Something like:
In C, arrays are homogeneous i.e. they can store data of a single type only. Even for multi-dimensional arrays. You possibly cannot have an array for all the data you describe without first wrapping it up in a
struct.You could then create an array of
rainfall_tand use it as you wish:You should also look up dynamically allocated memory in case you do not know up front how many locations you will have to deal with.