How to pass multi-dimensional array to function?

后端 未结 3 1998
猫巷女王i
猫巷女王i 2021-01-24 12:06

I am trying to pass a 2D array of variable size to a function to print it.but the code doesn\'t show the exact result of sum.

this is the code:

#include         


        
相关标签:
3条回答
  • 2021-01-24 12:33

    There is no technical problem with passing, but in sum_arr,
    your variable sum does not start at 0 (but some strange value).

    0 讨论(0)
  • 2021-01-24 12:46

    You should pass the exact size of the second dimension of the array to the function, not COLL. change it to m (or n, whatever) It passes the number 5 to the function while the number should be 3 :) How ever, this is not the main reason that you're code is not working, just a suggestion. Initialize the variable sum. It will make your code work. e.g. sum = 0; If you don't initialize it, you won't get any compile errors, but it points to a location of memory and reads thing been there before (not a valid amount) and uses it as the initial amount of that for sum. So your array is being added to a non-valid amount, that's why your code doesn't work.

    0 讨论(0)
  • 2021-01-24 12:49

    You have to initialize sum to zero in sum_arr function.

    0 讨论(0)
提交回复
热议问题