c - How to pass multi-dimensional array to function? -


I'm trying to exceed the 2D array of variable size in a function to print it. But the code does not show the correct result of the amount.

This code is:

  #include & lt; Stdio.h & gt; # Defined row 5 # COLL 5 void print_arr (int a [] [COLL], int m, int n) {int i, j, sum; (I = 0; i & lt; m; i ++) {for (j = 0; j & lt; n; j ++) {printf ("a [% d] [% d] =% d \ n ", i, j, a [i] [j]); }}} Int sum_arr (int a [] [COLL], int m, int n) {int i, j, sum; (I = 0; i & lt; m; i ++) for {for (j = 0; j & lt; n; j ++) {sum + = a [i] [j]; }} Refund Amount; } Int main (zero) {int one [ROW] [COLL]; Int i, j, m, n; Int sum; Printf ("Enter Rows:"); Scan ("% d", & amp; m); Printf ("Insert:"); scanf ("% d", & amp; n); (I = 0; i & lt; m; i ++) for {"a [% d] [% d] [% d] =", i, j) {for (j = 0; j & lt; n; j + +) {Scan ("% d", & a [i] [j]); }} Print_arr (a, m, n); Printf ("\ n"); Sum = sum_arr (one, m, n); printf ("sum =% d \ n", sum); Return 0; }   

This is the result of code

  Enter the rows: 2 enter: 3a [0] [0] = 5a [0] [ 1] = 8a [0] [2] = 4a [1] [0] = 7 a [1] [1] = 9 one [1] [2] = 6a [0] [0] = 5 a [0] [1] = 8a [0] [2] = 4a [1] [0] = 7 a [1] [1] = 9 a [1] [2] = 6 zodiac = -1217388517   

Please tell me what is wrong with the code ....

You should not give the exact size of the second dimension of the array on the function, not COLL to m (or n , whatever) Change this number to 5 , The number is 3 ) sometimes, this is not the main reason that the code is not working, only a suggestion variable sum is started Make this your code work like sum = 0; If you do not initiate it, you will not get any compilation errors, but it points to the location of memory and reads there before (no valid amount) and uses it for the amount In the form of initial amount, your array is being added to a non-valid amount, so your code does not work.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -