Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Why C treats array parameters as pointers?
C treats array parameter as pointers because it is less time consuming and more efficient. Though if we can pass the address of each element of the array to a function as argument but it will be more time consuming. So it’s better to pass the base address of first element to the function like:
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("\nCurrent 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("\nCurrent 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;
}
Output
Current content of the array is: 4 2 7 9 6 Current content of the array is: 4 2 7 9 6
Advertisements
