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

后端 未结 10 1432
终归单人心
终归单人心 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:52

    It's a fun feature of C that allows you to effectively shoot yourself in the foot if you're so inclined.

    I think the reason is that C is just a step above assembly language. Size checking and similar safety features have been removed to allow for peak performance, which isn't a bad thing if the programmer is being very diligent.

    Also, assigning a size to the function argument has the advantage that when the function is used by another programmer, there's a chance they'll notice a size restriction. Just using a pointer doesn't convey that information to the next programmer.

提交回复
热议问题