Hi all,

I 'm having a bit of trouble understanding about the use of malloc for 1d and 2d arrays., and I would appreciate if someone could tell me what is correct. Lets say for 1d arrays, which is correct?

orQuote:

vector = malloc(length*sizeof(vector));

and for 2d arrays:Quote:

vector = malloc(length*sizeof(*vector));

orCode:`matrix=malloc(rows*sizeof(matrix));`

for (i=0; i<columns; i++)

matrix[i]=malloc(columns*sizeof(matrix[i]));

I've tried both with gcc and all warnings/pedantic flags on, but both produce the same result (and no warnings) when I put some values in vector and array and then display them.Code:`matrix=malloc(rows*sizeof(*matrix));`

for (i=0; i<columns; i++)

matrix[i]=malloc(columns*sizeof(*matrix[i]));

I've also seen (assuming vector and matrix are unsigned integers

andCode:`vector = malloc(length*sizeof(unsigned int*))`

Ideally I'd like to avoid this in case I want to chenge the type of vector or matrix eg from float to double.Code:`matrix=malloc(rows*sizeof(unsigned int *));`

for (i=0; i<columns; i++)

matrix[i]=malloc(columns*sizeof(unsigned int));

Thank you

Spiros