In the C language, why does the following expression fail?
map = malloc(sizeof(Map) * tiles);
map = {
0,2,0,0,0,0,0,0,2,0,
0,1,0,0,0,0,0,0,1,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
2,1,0,0,0,0,0,0,1,2,
0,0,0,0,0,0,0,0,0,0
};
I just want to fill data in such a "row/column" format into a C array. However, the compiler fails with
error: expected expression before ‘{’ token
(in the map = { line). Filling the array in other ways works fine, and I am sure the brackets work for initializations...I can't do that after I have allocated memory?
EDIT: I solved it by making a temporary char array and then feeding the data to the malloced map in a for loop. Still. I'd like to know why the above code would leak memory as pointed below. And would my fix (parsing the temp array and setting the map data with it) leak memory as well?