Share | Tweet |
---|
Learn C array declaration, initilization, accessing array elements, using array examples, 2-dimensional arrays, passing arrays to function.
An array is a collection of variables of the same type. So instead of declaring individual variables one by one all can be declared once by using an array of same data type. For example instead of declaring 5 integers separately all can be declared as an integer array and can be accessed using an index.
An integer array contains couple of integers where as a character array holds few characters inside it as array elements.
Following are some array declaration, initialization examples.
Arr1 is an integer array holding 5 integer elements, Arr2 is a float array having 3 variables of float data type, Arr3 is a character array holding 5 characters. Note that in char carr[] = {'A','B','C','D','E'}; declaration we didn't give the number of elements the array can store. In this case compiler automatically computes the size required to store all the elements of the array in memory and allocates memory space accordingly.
int Arr1[5] = {1,2,3,4,5}; /* int array holding 5 integers */
float Arr2[3] = {0.2,3.5,4.0}; /* float array holding 3 float elements */
char carr[] = {'A','B','C','D','E'}; /* char array holding 5 char elements */
To demonstrate the above features, let's write a small program as shown below
#include "stdio.h"
void main()
{
printf("=====Integer Array====\n");
int iarr[5] = {0,1,2,3,4};
int i;
printf("Address of first element:%x,%x\n",iarr,&iarr[0]);
for(i = 0;i < 5;i++)
{
printf("iarr %x and %x\n",&iarr[i],iarr + i);
printf("iarr element:%d, %d\n",i,iarr[i]);
}
printf("=====Character Array====\n");
char carr[5] = {'A','B','C','D','E'};
for(i = 0;i < 5;i++)
{
printf("carr %x and %x\n",&carr[i],carr + i);
printf("carr element:%d and %c\n",carr[i],carr[i]);
}
}
Program output.$ gcc prog.c $ ./a.out =====Integer Array==== Address of first element:28fee8,28fee8 iarr 28fee8 and 28fee8 iarr element:0, 0 iarr 28feec and 28feec iarr element:1, 1 iarr 28fef0 and 28fef0 iarr element:2, 2 iarr 28fef4 and 28fef4 iarr element:3, 3 iarr 28fef8 and 28fef8 iarr element:4, 4 =====Character Array==== carr 28fee3 and 28fee3 carr element:65 and A carr 28fee4 and 28fee4 carr element:66 and B carr 28fee5 and 28fee5 carr element:67 and C carr 28fee6 and 28fee6 carr element:68 and D carr 28fee7 and 28fee7 carr element:69 and E $
In above program we have tried to access elements from integer array iarr[5]. Notice that the addresses of iarr[0] is 28fee8 which is same as name of array iarr. Also notice that the address of each successive element increases by 4 which means that integer takes 4bytes of space in memory for this machine. Similarly for the character array carr addresses are incremented by 1 as each character takes one byte of space in memory.
We pass an array to a function by reference. Hence if we update any array element inside the function it will be reflected in the calling function as well. This is demonstrated in the following example program.
#include "stdio.h"
#define SIZE 5
int sumArr(int iarr[], int res[]);
void main()
{
int iarr[SIZE] = {0,1,2,3,4};
int sum[2] = {0,0};
sum[0] = sumArr(iarr,sum);
printf("Sum is:%d,%d\n", sum[0],sum[1]);
}
int sumArr(int iarr[],int res[])
{
int i, s = 0;
for(i = 0;i < SIZE;i++)
{
s += iarr[i];
res[1] += iarr[i];
}
return s;
}
Program output.$ gcc prog.c $ ./a.out Sum is:10,10 $
In the above program we calculated the sum of all numbers in iarr in two ways. One way by returning the sum from function sumArr and another way by updating the 2nd element of sum array. Notice from the program output that both outputs are same.
Two dimensional arrays are nothing but an array of arrays. In the above example program ar[0] is nothing but the first array of 2-d integer array ar and ar[1] is nothing but the second array of 2-d integer array ar.Share | Tweet |
---|
Search more C examples |
Search more C MCQs |
Git Version Control |
Linux OS Internals |
Linux Processes and Threads |
Linux OS Commands |
Linux IPC Mechanisms |
LCM and HCF of numbers |
Series completion |
Time and Distance |
UART Protocol & Applications |
SPI Protocol & Applications |
I2C Protocol & Applications |
Microcontrollers, Tools & Peripherals |