Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Creating Variable-size 2D Arrays (http://www.go4expert.com/forums/creating-variable-size-2d-arrays-t9458/)

nickld 19Mar2008 16:04

Creating Variable-size 2D Arrays
I'm writing a program in C to multiply two user-inputted matrices and am having a little bit of trouble initializing the input arrays...

Basically, the program starts with the user inputting the dimensions of each matrix (matrix1 and matrix2) and then these values are stored as integers in the variables rowsize1, columnsize1, rowsize2, columnsize2.

I now want create these two matrices... However I cannot do this with matrix1[rowsize1][columnsize2] and matrix2[rowsize2][columnsize2] as this is illegal in C!

I read that variable-size arrays can be created in C by using the "malloc" function but I'm not entirely sure on how to implement it in my case...

For the first matrix (matrix1) would I have to do something like this:

int **matrix1;
matrix1 = (int**)malloc(columnsize1*sizeof(int*));

for(a = 0; a < rowsize1; ++a)
matrix1[a] = (int*)malloc(sizeof(int));

I'm not really sure though...

Any help would be much appreciated!!

All times are GMT +5.5. The time now is 17:43.