#C treats array parameters as pointers because it is less time consuming and more efficient. Although we could pass the address of each element of the array as a parameter to the function, doing so would be more time-consuming. So it's better to pass the base address of the first element to the function, for example:
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("</p><p>Current content of the array is: </p><p>"); for(i = 0; i < 5; i++) printf(" %d",a[i]); } void display2(int *a) //printing the array content { int i; printf("</p><p>Current content of the array is: </p><p>"); 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; }
Output
Current content of the array is: 4 2 7 9 6 Current content of the array is: 4 2 7 9 6
The above is the detailed content of Why does C treat array parameters as pointers?. For more information, please follow other related articles on the PHP Chinese website!