Ways to create dynamic matrix in C?
31,422
Solution 1
You can try like this
int main()
{
int i, j, lines, columns, *intMatrix;
printf("Type the matrix lines:\t");
scanf("%d", &lines);
printf("Type the matrix columns:\t");
scanf("%d", &columns);
intMatrix = (int *)malloc(lines * columns * sizeof(int));
for (i = 0; i < lines; ++i)
{
for (j = 0; j < columns; ++j)
{
printf("Type a number for <line: %d, column: %d>\t", i+1, j+1);
scanf("%d", &intMatrix[i*lines + j]);
}
}
Solution 2
From C99 onwards (but not C++), you can use variable length arrays:
int main()
{
int i, j, lines, columns;
printf("Type the matrix lines:\t");
scanf("%d", &lines);
printf("Type the matrix columns:\t");
scanf("%d", &columns);
{
int intMatrix[lines][columns];
for (i = 0; i < lines; ++i)
{
for (j = 0; j < columns; ++j)
{
printf("Type a number for <line: %d, column: %d>\t", i+1, j+1);
scanf("%d", &intMatrix[i][j]);
}
}
}
}
Or even like this:
void readData (int lines, int columns, int array[lines][columns])
{
int i, j;
for (i = 0; i < lines; ++i)
{
for (j = 0; j < columns; ++j)
{
printf("Type a number for <line: %d, column: %d>\t", i+1, j+1);
scanf("%d", &array[i][j]);
}
}
}
int main()
{
int lines, columns;
printf("Type the matrix lines:\t");
scanf("%d", &lines);
printf("Type the matrix columns:\t");
scanf("%d", &columns);
{
int intMatrix[lines][columns];
readData (lines, columns, intMatrix);
}
}
But, in both cases, the array data is all stored on the stack, not the heap, so there's no way to store it properly, and you can't put it in a struct or anything malloc'd.
Author by
tempy
Updated on July 15, 2022Comments
-
tempy almost 2 years
This is the only way I know to create a matrix (2D array) in C, dynamically, and reading user input into its elements:
Creating a pointer to an array of
x
pointers, where each pointer represents a line in the matrix -x
is the number of lines in the matrix (its height).Pointing each pointer in this array to an array with
y
elements, wherey
is the number of columns in the matrix (the width).
int main() { int i, j, lines, columns, **intMatrix; printf("Type the matrix lines:\t"); scanf("%d", &lines); printf("Type the matrix columns:\t"); scanf("%d", &columns); intMatrix = (int **)malloc(lines * sizeof(int *)); //pointer to an array of [lines] pointers for (i = 0; i < lines; ++i) intMatrix[i] = (int *)malloc(columns * sizeof(int)); //pointer to a single array with [columns] integers for (i = 0; i < lines; ++i) { for (j = 0; j < columns; ++j) { printf("Type a number for <line: %d, column: %d>\t", i+1, j+1); scanf("%d", &intMatrix[i][j]); } }
Are there other ways to do this?
-
tempy over 11 yearsIs C99 popular in C programming nowadays? (I'm currently using C85) but I don't know what real C programmers use today (in the industry).
-
ams over 11 yearsThere's a C85? I'm aware of C89 (aka C90), C99, and C11. Real code uses whatever the compiler allows, standard or otherwise. :)
-
tempy over 11 yearsHow can I pass the matrix to a function in both my example (
**intMatrix
) and yours (*intMatrix
)? How will I access the matrix elements inside the function? Thanks. -
Raj over 11 yearsyou can pass to the function as
int *matrix
and then calculate the element asintMatrix[i*lines + j]
as shown in the example.