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

后端 未结 7 887
北海茫月
北海茫月 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 18:57

    Note that in C++, if the length of the array is known at compile time (for example if you passed a string literal), you can actually get its size:

    template
    void func(const char(&str)[N])
    {
        // Whatever...
    }
    
    int main()
    {
        func("test"); // Works, N is 5
    }
    

提交回复
热议问题