An array is a collection of data items of the some type. Items are store at contiguous memory location. It can also store the collection of derive data types, such as Pointer, structures, etc. A one dimensional array is like a list. A two dimensional array is like a table. The C languages places on limit on the number of dimensions in an array. Some texts refer to one dimensional array as vectors, and Two dimensional array as matrices and use the general term array when the number of dimensions is unspecified or unimportant. The name of the array represent by square brackets [].

Types of Array

  • One dimensional array
  • Two dimensional array

One dimensional Array

Only one row for storing data is knows as One dimensional Array.

Declaration of C Array

  • data_type array_name[array_size];  

Example of Declaration of C array

int marks[42]

Initialization of C Array

An array can be initializes in two ways

  • Compile time initialization
  • Runtime initialization

Compile time initialization

Example

#include<stdio.h>
int main ( ){
   int x[6] = {100 , 200, 300 , 400 , 500 , 600 };
   int y;
   printf ("elements of the array");
   for ( y=0; y<6; y++)
      printf ("%d \n", x[y]);
}

Output

Output value

Runtime initialization

Example

#include<stdio.h>
main ( ){
    int x[5],i;
    printf ("enter 5 elements");
    for ( i=0; i<5; i++)
        scanf("%d\n", &x[i]);
    printf("elements of the array are");
    for (i=0; i<5; i++)
        printf("%d\n ", x[i]);
}

Output

Input value

Example

C program for one dimensional array

input value

#include <stdio.h>
int main(void){
    int x[3];
    int y[3] = {1};
    int z[3] = {1,2,3};
    int i;
    printf("\nArray x:\n");
    for( i=0; i<3; i++ )
        printf("rom[%d]: %d\n",i,x[i]);
        printf("\nArray y:\n");
    for( i=0; i<3; i++)
        printf("rom[%d]: %d\n",i,y[i]);
    printf("\nArray z:\n");
    for( i=0; i<3; i++ )
        printf("rom[%d]: %d\n",i, z[i]);
    return 0;
}

Output

Output value

Two Dimensional Array

Two or more than two row is called multi dimension array. It is also known as Matrix.

Declaration of two dimensional Array

data_type array_name[rows][columns];

Example

int twodimen[4][3];  

Initialization of 2D Array

An array can be initializes in two ways

  • Compile time initialization
  • Runtime initialization

Compile time initialization

Example

input value

#include<stdio.h>  
int main(){      
int x=0,y=0;    
int arr[4][3]={{4,5,6},{7,8,9},{1,2,3},{4,5,6}};     
    
for(x=0;x<4;x++){    
 for(y=0;y<3;y++){    
   printf("arr[%d] [%d] = %d \n",x,y,arr[x][y]);    
 }    
}   
return 0;  
}    

output

OUTPUT VALUE

Runtime initialization

Example

Input value

#include <stdio.h>    
void main ()    
{    
    int arr[4][4],x,y;     
    for (x=0;x<4;x++)    
    {    
        for (y=0;y<4;y++)    
        {    
            printf("Enter p[%d][%d]: ",x,y);                
            scanf("%d",&arr[x][y]);    
        }    
    }    
    printf("\n printing the elements ....\n");     
    for(x=0;x<4;x++)    
    {    
        printf("\n");    
        for (y=0;y<4;y++)    
        {    
            printf("%d\t",arr[x][y]);    
        }    
    }    
}    

Output

Output value

Return array to function

C programming does not allow to return an entire array as an argument to a function is knows as Return array .

There are three right ways of returning an array to a function:

  • Dynamically allocated array
  • Static array
  • Structure array

Example

#include <stdio.h>
int* getEvenNumbers(int N){
    static int evenNumberArray[100];
    int i, even = 2;
     
    for(i=0; i<N; i++){
        evenNumberArray[i] = even;
        even += 2;
    }
    return evenNumberArray;
}
 
int main(){
   int *array, counter;
   array = getEvenNumbers(20);
   printf("Even Numbers\n");
   for(counter=0; counter<20; counter++){
       printf("%d\n", array[counter]);
   }
    
   return 0;
}

Ouput

OUTPUT VALUE

Passing Array to Function in C

Syntax

functionname(arrayname);

Methods to declare a function

There are three ways:-

First Way

return_type function(type arrayname[]) 

Second Way

return_type function(type arrayname[SIZE])

Third Way

return_type function(type *arrayname)  

Example

Input Value

#include<stdio.h>  
int minarray(int arr[],int size){    
int min=arr[0];    
int i=0;    
for(i=1;i<size;i++){    
if(min>arr[i]){    
min=arr[i];    
}    
}   
return min;    
}    
    
int main(){      
int i=0,min=0;    
int numbers[]={52,57,70,325,84,97,40};  
  
min=minarray(numbers,7);   
printf("minimum number is %d \n",min);    
return 0;  
}    

Output

Output value

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about C Programming language please Wikipedia click here .

Stay Connected Stay Safe, Thank you


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *