Difference between passing array, fixed-sized array and base address of array as a function parameter

后端 未结 7 858
北海茫月
北海茫月 2020-12-07 18:29

I am confused about which syntax to use if I want to pass an array of known or unknown size as a function parameter.

Suppose I have these variants for the purpose:

相关标签:
7条回答
  • 2020-12-07 19:17

    To add-on, describing in points.

    1) As everyone told it is same.

    2) Arrays are decayed into pointers when they are passed in the function arguments.

    3) Fundamental problem could be finding the size of a array in the function. For that we can use macro like.

       #define noOfElements(v) sizeof(v)/sizeof(0[v])
    
       int arr[100]
       myfunction ( arr, noOfElements(arr))
    

    either 0[v] or v[0] can be used in the macro, where the first is used to avoid user defined data type passed in to noOfElements.

    Hope this helps.

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