据我所知
int func(int arr[5]) {
    printf("Test");
}
Run Code Online (Sandbox Code Playgroud)
方括号中的数组大小(例如 5)没有任何意义,因为 arr 只是指向数组第一个元素的指针,即它与
int func(int *arr) {
    printf("Test");
}
Run Code Online (Sandbox Code Playgroud)
或者
int func(int arr[]) {
    printf("Test");
}
Run Code Online (Sandbox Code Playgroud)
但是,如果数字本身没有任何意义,为什么可以在方括号中写入像 5 这样的数字呢?
确实,数组符号被调整(“衰减”)为指向第一个元素的指针,使所有 3 个示例等效。该语言的设计方式是为了使表达式中的数组使用与函数兼容,因为当在大多数表达式中使用数组时,它也会“衰减”为指向第一个元素的指针。
不过,尺寸确实有一定的意义:
您可以通过声明参数来稍微提高类型安全性int arr[static 5],其中static意味着传递至少5 个项目而不是空指针。“衰退”仍然会发生,但现代编译器可能会给出诊断消息:
#include <stdio.h>
void func(int arr[static 5]) 
{
  printf("Test");
}
int main(void)
{
  int a[5];
  func(a); // ok
  int* b;
  func(b); // accepted
  int c[4];
  func(c); // not ok
  func(NULL); // not ok
}
Run Code Online (Sandbox Code Playgroud)
您还可以将参数声明为指向 array 的指针int (*arr)[5]。这进一步提高了类型安全性,因为这意味着必须传递恰好 5 个 int 的数组。但这使得该函数使用起来更加麻烦,因为您必须在使用该参数时取消引用该参数(*arr)[i]。数组参数必须作为 传递func(&a)。