Why do C and C++ compilers allow array lengths in function signatures when they're never enforced?

后端 未结 10 1410
终归单人心
终归单人心 2020-11-22 06:59

This is what I found during my learning period:

#include
using namespace std;
int dis(char a[1])
{
    int length = strlen(a);
    char c = a         


        
10条回答
  •  清酒与你
    2020-11-22 07:39

    It is a quirk of the syntax for passing arrays to functions.

    Actually it is not possible to pass an array in C. If you write syntax that looks like it should pass the array, what actually happens is that a pointer to the first element of the array is passed instead.

    Since the pointer does not include any length information, the contents of your [] in the function formal parameter list are actually ignored.

    The decision to allow this syntax was made in the 1970s and has caused much confusion ever since...

提交回复
热议问题