
C将数组参数视为指针,因为这样做更省时且更高效。尽管我们可以将数组的每个元素的地址作为参数传递给函数,但这样做会更耗时。所以最好将第一个元素的基地址传递给函数,例如:
void fun(int a[]) {
…
}
void fun(int *a) { //more efficient.
…..
}Here is a sample code in C:
#include
void display1(int a[]) //printing the array content
{
int i;
printf("Current content of the array is:
");
for(i = 0; i < 5; i++)
printf(" %d",a[i]);
}
void display2(int *a) //printing the array content
{
int i;
printf("
Current content of the array is:
");
for(i = 0; i < 5; i++)
printf(" %d",*(a+i));
}
int main()
{
int a[5] = {4, 2, 7, 9, 6}; //initialization of array elements
display1(a);
display2(a);
return 0;
}
输出
Current content of the array is: 4 2 7 9 6 Current content of the array is: 4 2 7 9 6










