Tell me please, what am I doing wrong? You need to enter and display the matrix, that's it.

int main() { int* matr; int n,m; scanf("%i",&n); scanf("%i",&m); matr = malloc(m*n*sizeof(int)); for (int j = 0; j < m; ++j) { for (int i = 0; i < n; ++i) { scanf("%i",&matr[j,i]); } } for (int j = 0; j < m; ++j) { for (int i = 0; i < n; ++i) { printf("%2i",matr[j,i]); } puts(""); } return 0; } Ввод: 3 3 1 2 3 4 5 6 7 8 9 Вывод: 7 8 9 7 8 9 7 8 9 

UPD:

 int printline(int* line){ for (int j = 0; j < n; j++) { printf("%2i ",line[i]); } } 

Enter:

 3 4 2 1 0 3 5 6 7 9 8 1 5 2 

Conclusion :

 5 6 -1414812757 

    1 answer 1

    Let me show you how:

     int main() { int** matr; int n,m; scanf("%i",&n); scanf("%i",&m); matr = malloc(m*sizeof(int*)); for (int j = 0; j < m; ++j) { matr[j] = malloc(n*sizeof(int)); for (int i = 0; i < n; ++i) { scanf("%i",&matr[j][i]); } } for (int j = 0; j < m; ++j) { for (int i = 0; i < n; ++i) { printf("%2i",matr[j][i]); } puts(""); } } 

    Only in an amicable way, it is also necessary to free the allocated memory.

    • Suppose, and why I have not correctly, because logically correct - buciki
    • @buciki read like this and similarly output scanf ("% i", & matr [j * m + i]); ( ideone.com/45dHC0 ) - uber42
    • Wrong. At least because [j,i] means in essence [i] - read the textbooks ... - Harry
    • @Harry Well, look, but how to transfer and process the row of the matrix in a function? I myfunc(matr[2]) //3-ая строка so myfunc(matr[2]) //3-ая строка , then, in the function I refer to the element so printf("%i ",line[1]) //2-ой элемент , but unfortunately the error is buciki
    • void myfunc(int*a){ printf("%i",a[1]); } void myfunc(int*a){ printf("%i",a[1]); } - function, myfunc(matr[2]) - call (I see the matr described in my code). - Harry