Why am I being told that an array is a pointer? What is the relationship between arrays and pointers in C++?

后端 未结 6 1056
情书的邮戳
情书的邮戳 2020-12-02 02:39

My background is C++ and I\'m currently about to start developing in C# so am doing some research. However, in the process I came across something that raised a question abo

6条回答
  •  醉酒成梦
    2020-12-02 03:01

    Arrays and pointers in C and C++ can be used with the exact same semantics and syntax in the vast majority of cases.

    That is achieved by one feature:

    Arrays decay to pointers to their first element in nearly all contexts.
    Exceptions in C: sizeof, _Alignas, _Alignas, address-of &
    In C++, the difference can also be important for overload-resolution.

    In addition, array notation for function arguments is deceptive, these function-declarations are equivalent:

    int f(int* a);
    int f(int a[]);
    int f(int a[3]);
    

    But not to this one:

    int f(int (&a)[3]);
    

提交回复
热议问题