c - Why doesn't work the matrix with 5,3 but 3,5? -


i've written code, don't know why fails 5 3 input. when give 3 5 works fine. in every case, if second number bigger, works, why? i've tried malloc. i'm using windows 7, code blocks 10.05

#include<stdio.h> #include<stdlib.h>  int main() {     int **matr;     int row, col, i, j;      scanf("%d", &row);    //number of rows     scanf("%d", &col);    //number of cols      matr = calloc(col, sizeof(int*));      //creating cols     for(i = 0; < col; i++)     {         matr[i] = calloc(row, sizeof(int));  //in every col create array size of row     }      for(j = 0; j < row; j++)     {         for(i = 0; < col; i++)         {             matr[j][i] = 10;        //fill matrix number 10         }     } printf("matrix ready\n");      for(j = 0; j < row; j++)        //printing matrix     {         for(i = 0; < col; i++)         {             printf("%d ", matr[j][i]);         }         printf("\n");      }      return 0; } 

you correctly allocated matrix of size [col][row]. , access if matrix of size [row][col]. no wonder crashes.

first decide index interface want use - [col][row] or [row][col] - , either change allocation of change access match.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -