Take the 2-minute tour ×

I'm trying to make a struct that contains another struct with multiple arrays. I need to dynamically allocate those arrays too, so I think I need another pointer still.

int arraysize;

typedef struct Array{
int *size = arraysize;
unsigned int val[*size];
unsigned int x[*size];
unsigned int y[*size];
} Array;

typedef struct Image{
int height;
int width;
int max;
Array *data;
} Image;

OK, so once I finally figure that out, I still need to figure out how to dynamically allocate that memory using malloc. I'm totally lost there too. Any help at all would be greatly appreciated.

EDIT: more clarification: I'm using the arrays to store three pieces of information that are all connected. Think of a chessboard, you could say knight E4, which tells you that on the 4th column of row E, there is a knight. If you started this process at A1 and ended at K10 you'd have a full chessboard right? The image struct is analogous to the chessboard, the Array is analogous to a list of a bunch of squares that compose the chessboard and the contents of those squares. (E.g. A1 null A2 knight a3 bishop etc...) Unfortunately, I don't know what kind of board will be passed through, it might be a 3x7 board or a 9x2 board etc. So I need to dynamically allocate the memory for those possibilities. Once I have the memory allocated I need to store information about the location and the contents of all of the "squares." Then I need to let a program pass through the height of the board, width of the board and the list of contents and I'd be done the hard part.

share|improve this question
I think you need to take this one step at a time. Find out how to do a single dynamically allocated array first. –  Vaughn Cato Sep 24 '13 at 22:13
There is a tutorial here that should answer your questions. –  ryyker Sep 24 '13 at 22:16
The most glaring mistake is the first one is not valid code. The second is also invalid, but only because the first one is likewise. –  WhozCraig Sep 24 '13 at 22:19
What exactly is your question after all? –  Filipe Gonçalves Sep 24 '13 at 22:22
A structure that contains multiple arrays is naturally called MultipleArrays and not Array, innit? –  n.m. Sep 24 '13 at 22:25

1 Answer 1

What you actually meant was:

typedef struct data {
    unsigned int x;
    unsigned int y;
    unsigned int val;
} Data;

typedef struct image {
    int height;
    int width;
    int max;
    Data* data;
} Image;

and somewhere:

Image i;
i.height = 10;
i.width  = 20;
i.data   = malloc(sizeof(Data) * i.width * i.height);
// one of the ways how to access Data at 2nd row, 3rd column:
*(i.data + i.width * 1 + 2).val = 7;
i.data = NULL;

But what you actually need is some good book ;)

share|improve this answer
^ Doesn't that only put in 1 set of data values for each image? I need one set of "Data" per height*width (E.G, if height is 2 and width is 2 there are four different locations where I need to store the x y and val of the Data.) Only I need height and width to be variables so I don't know how many sets of Data I need. I tried to build a struct that compiles the three pieces of information in arrays with pointers so that I could dynamically plug in the size when the program gets it and then fill in the arrays with meaningful data. I just don't know how to implement this the right way. –  TheDebaser Sep 24 '13 at 23:59
@TheDebaser: But that's something different that you described in original question. See my edit. –  LihO Sep 25 '13 at 5:47

Your Answer


