为什么C将数组参数视为指针?
C将数组参数视为指针,因为它耗时少且效率高。尽管如果我们可以将数组每个元素的地址作为参数传递给函数,但这将更加耗时。因此,最好将第一个元素的基地址传递给函数,例如:
void fun(int a[]) {
…
}
void fun(int *a) { //more efficient.
…..
}这是C语言中的示例代码:
#include
void display1(int a[]) //printing the array content
{
int i;
printf("\nCurrent content of the array is: \n");
for(i = 0; i < 5; i++)
printf(" %d",a[i]);
}
void display2(int *a) //printing the array content
{
int i;
printf("\nCurrent content of the array is: \n");
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
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短