C Array is a collection of similar type of variables. You can store group of data of same data type in an array.
To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows −
type arrayName [ arraySize ];
This is a single-dimensional array.
The arraySize must be an integer constant greater than zero and type can be any valid C data type.
For example, to declare a 10-element array called ar of type integer, use this statement −
int ar[10];
Here ar is a variable array which is sufficient to hold up to 10 integer numbers.
Passing entire one-dimensional array to a function
There are two possible ways to pass the array to function, one will lead to call by value and the other is used to perform call by reference.
1. We can either have an array in parameter.
a. void sum (int arr[]);
2. We can have a pointer in the parameter list, to hold base address of array.
a. void sum (int* ptr);
1. We can either have an array in parameter.
While passing arrays to the argument, the name of the array is passed as an argument
#include <stdio.h>
#include <stdio.h>
#define SIZE 10
void display(int a[]); //function prototype with parameter as array without size
void main()
{
int ar[SIZE],i;
clrscr();
for(i=0;i<SIZE;i++)
{
printf(“Insert Element\n”)
scanf(“%d”,&ar[i]);
}
display(ar); //calling the function and passing array name as argument
getch();
}
void average(int a[])//function defination with parameter as array without size
{
int i;
for(i=0;i<SIZE;i++)
{
Printf(“Array Elements : %d \n”,ar[i]);
}
}
|
0 comments:
Post a Comment