int* 和 int*[] 有什么区别

Iva*_*nov 2 c++

void test(int* integers, int n) { ... }
void test(int* integers[], int n) { ... }
Run Code Online (Sandbox Code Playgroud)

有什么不同?它们都是指向 int 的指针,并且可以用作数组。例如,什么时候使用第一个?

Ale*_*rov 5

int*是一个指向 的指针int。它不是数组,但它可能表示数组的起始地址(或数组迭代器 \xe2\x80\x94 数组内部的位置)。

\n\n

int*[]是一个指针数组int不是s数组int!)。因为我们处于函数参数的上下文中,所以可以有空括号(即未知大小的数组),这实际上使该参数成为指向 的指针int

\n\n
\n\n

void test(int* integers)您的意思是“和”之间有什么区别吗void test(int integers[])

\n\n

作为函数参数type arg[]type arg[SIZE]对于编译器来说实际上type* arg是相同的。type* arg

\n\n

对于阅读你的代码的程序员:

\n\n
    \n
  • type arg[]给出一个提示arg应该是一个代表数组起始地址的指针。

  • \n
  • type arg[SIZE]给出一个提示arg应该是一个指针,表示指定的数组的起始地址SIZE。请注意,编译器会忽略SIZE并且不执行任何边界检查。

  • \n
\n

  • 在这种情况下,“int*[]”*不是*数组,绝对不是。即使指定了尺寸也不行。 (2认同)