我有一个原型(prototype)为的函数

void test( int array [] , int b);

我知道我们可以将原型(prototype)替换为:void test(int*, int);
main() 中,我们声明了以下数组:
int array1[10], array2[10];

要将函数体设置为 0,
test ( array1 , b)
{
for ( int i = 0 ; i < b ; i++)
  array1[i] = 0;

}

我可以做以下吗?为什么?
int main()
{// assuming b is the size of the array
test(array1 , b);
test(array2 , b) ;
return 0;
}

我知道 C++ 的基本知识,我正在尝试编写自己的包含文件。
我只是想知道这是否可能,这是一个不错的选择吗?

最佳答案

您可能会问形式参数和实际参数之间的区别。

在你的原型(prototype)中

void test(int *array, size_t size);

名称 'array' 和 'size' 是形式参数。您在函数体内使用这些名称。

在调用函数的代码中,可以使用不同的名称,即实际参数。

所以
int main()
{
   const size_t b = 10;
   int array1[10], array2[10];
   test(array1 , b);
   test(array2 , b) ;
   return 0;
}

这里 array1b 是第一次调用的实际参数, array2b 是第二次调用的实际参数。

所以是的,您可以使用任何您喜欢的名称作为实际参数,只要变量的类型与您的原型(prototype)相匹配。

关于c++ - 如何调用C++函数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6568300/

10-11 22:42
查看更多