C Arrays

C array

array is an data structure there are used to combine group of elements. c array are contain similar type of elements. array size is important factor. because size is decide how many number of element are array will store. the size of array is decided when we are declare an array.

Syntax of array

data_type tag_name[size_of_array];
Iteam overview
data_type Use predefined and user defined data type
tag_name Name of Array
size_of_array Any positive integer value

For example

int data[10];

This data is an array of 10 integers.

Initializing Arrays

    //integer array
  int data[10]={1,2,3,4,5,6,7,8,9,10};

For example

/* 
  Example of Array
*/
/*Header file*/
#include <stdio.h>

int main(){
  //assign array elements
  int data[10]={1,2,3,4,5,6,7,8,9,10};
    int index=0;
    //print array elements
  for(index;index<10;index++){
    printf("data[%d] : %d \n",index,data[index]);
  }
    return 0;
}
C print array elements

Output

data[0] : 1 
data[1] : 2 
data[2] : 3 
data[3] : 4 
data[4] : 5 
data[5] : 6 
data[6] : 7 
data[7] : 8 
data[8] : 9 
data[9] : 10

Note that if we are declare the size of array they are not change during the program.

There are following possibility to declare c array.

/* 
  Example of array declaration
*/
/*Header file*/
#include <stdio.h>

int main(){
  //create char array 
  char language[]={'C',' ','P','r','o','g','r','a','m','m','i','n','g'};
  printf("language : %s\n",language );
  char book[]="Data structure";
  printf("book : %s\n",book );
  char boolean[2]={'1','0'};
  printf("boolean : %s\n",boolean );
  return 0;
}

Output

language : C Programming
book : Data structure
boolean : 10

Multi-dimensional arrays

In c language we can declare and use of multidimensional array. look at view this example. This is 2d array. we can also called (array of array). similar way we can create any number of multidimensional in c array

int data[size][size]; //2d arrays  

For example create 3d arrays.

Syntax

  data_type array_name[size][size][size];//3d arrays

For example

/* 
  Example of multidimensional arrays
*/
/*Header file*/
#include <stdio.h>

int main(){
  //create multidimensional array and assign data value
  int record[2][2][3]={
    {
      {1,2,3},
      {4,5,6},
    },
    {
      {10,20,30},
      {40,50,60}
          
    } 
  };
  int i=0,j=0,k=0;
  //print 3d array
  for(i;i<2;i++){
    for (j=0;j<2;j++){
      for(k=0;k<3;k++){
        //print array elements
        printf("record[%d][%d][%d] : %d\n",i,j,k,record[i][j][k]);
      }
    }
  }
  return 0;
}
Multidimensional arrays

Output

record[0][0][0] : 1
record[0][0][1] : 2
record[0][0][2] : 3
record[0][1][0] : 4
record[0][1][1] : 5
record[0][1][2] : 6
record[1][0][0] : 10
record[1][0][1] : 20
record[1][0][2] : 30
record[1][1][0] : 40
record[1][1][1] : 50
record[1][1][2] : 60

Passing arrays to functions

There are many way to pass array to function. look at this example.
1)Passing array reference to function

/* 
  Example of array passing to function
*/
/*Header file*/
#include <stdio.h>
//passing array to function
void print_info(int arr[], int size){
  if(size>1){

    int index=0;
    for(index;index<size;index++){
      printf("%d\n",arr[index] );//print array elements
    }
  }
}
int main(){
  int store[]={1,2,3,4,5};
  //get size of array
  int size=(sizeof(store))/sizeof(store[0]);
  print_info(store,size);
  return 0;
}

in this example we are send main function array to another function print_info(). print_info are accept two parameter one is integer array and second one is size of array. so visualize this process in given diagram.

Passing arrays to function

Output

1
2
3
4
5

Note that there are not possible to pass complete copy (new copy) of array to function. we are passing pieces of element value. and passing reference of array to function. for example in this case passing reference.

//Every statements have same meaning
  void print_info(int *arr, int );/* arr is a pointer */
  void print_info(int arr[], int );/* arr is a pointer */
  void print_info(int arr[5], int );/* arr is a pointer of 5 integer */

2) Passing single element of an array to function

/* 
  Example Passing single element of an array to function
*/
/*Header file*/
#include <stdio.h>
//passing array element into function
void alphabet(char data){
  printf("char is :%c\n",data);
}
int main(){
  //array elements
  char list[]={'A','B','C','D'};
  alphabet(list[3]);
  alphabet(list[1]);
  return 0;
}
Passing Array Value

Output:

char is :D
char is :B

3) Passing Multi-dimensional Arrays to Function.

/* 
  Example Passing 2d array
*/
/*Header file*/
#include <stdio.h>

int rows=3,colos=2;
//print array element
void display(int arr[rows][colos]){
  int i=0,j=0;
  //print 2d array
  for (i = 0; i < rows; i++){
    for(j=0;j<colos;j++){
      printf("arr[%d][%d]: %d\n",i,j,arr[i][j]);
    }
  }
  
}
int main(){
  //2d array
  int data[3][2]={
    {0,1},{0,1},{0,1}
  };
  //passing array
  display(data);

  return 0;
}
Passing Multi-dimensional array

Output

arr[0][0]: 0
arr[0][1]: 1
arr[1][0]: 0
arr[1][1]: 1
arr[2][0]: 0
arr[2][1]: 1

© 2019, regularcodes.com, All rights reserved