C Arrays

Array in c programming language there are collection of similar type of data element.This is very useful to store same type of information. When we declare a array there are need to informative values. like how many number of elements it will stored this array, what is the type of elements, and some cases we need to set then provide initial values.

C array

There are following most important point of array

Array element are access by index value. Index of array will start by zero (0) and last element of array is accessible by (element-1) index value. Memory of array are is allocated on contiguous manner. So that way we can access any element of array by its index. Array are single and multidimensional possible but that array can be stored only same type of element. That are decided by its defining data type. We cannot create an empty or zero size 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

We can define of any type of array variables by using of predefined data type. here given a few examples.

int data[10];//data is array of 10 integers
char status[5];//status is array of 5 character
int *ptr[7];//ptr is an array of 7 integer pointer
float amount[5];//amount is an array of 5 element in float type

Note that the size of array is compulsory when we are not initialize array element. And size of array element are fixed that are not modified when it's define.

How to store array element

Initializing: that is initialization process of array element. we can define of following way.

//my_element is an integer array of 5 elements
int my_element[]={1,2,3,4,5};

In this example we are not provide the size of array. but provide the value of elements. So in this situation compiler is define the size of array element using of number of elements. In this situation compiler are understand what is the size of array. We can find size of Single dimensional array as follows.

//Get the size of array in C
#include<stdio.h>

int main(){
  //my_element is an integer array of 5 elements
  int my_element[]={1,2,3,4,5};
  //get the size of array
  int size=sizeof(my_element)/sizeof(int);

  printf("Array size :%d\n",size );
  return 0;
}

Output

Array size :5

In this example sizeof(array_name) is return 20 for 32 bit compiler and 16 bit compiler are provide 10. because 2 bit integer and 5 array element are produce 10 the bytes of array size. and similarly 4 bit integer compiler are produce the (4*5) =20 bytes memory size. And after that we are divide this bytes size by size of integers and get the actual number of elements.

Second way to providing the array size and its elements values.

//provide a size and its elements
int data[7]={1,2,3,4,5,6,7};


//Array of 5 integer
//first element value is set one
//then remaining elements value are zero
int info[5]={1};

In first statement are define an array of integers there size is 7 and provide the 7 integer elements. So that is simple statement and every thing is clear. But second statement are interesting, here define the size but not provided all element value only set first element value.

In this situation compiler are set first element value and remaining element are assign to zero because that is an integer array. See this example.

#include<stdio.h>

int main(){
  //data is an array of 5 integers
  int data[5]={1};
  //get the size of array
  int size=sizeof(data)/sizeof(int);

  int index=0;
  //Display array elements
  for(index;index<size;index++){
    printf("%d ",data[index] );
  }
 
  return 0;
}
Output
1 0 0 0 0

When we are assign all elements value at initially. then compiler are not set any default value. See this example.

/* 
  Example of array initialization
*/
/*Header File*/
#include <stdio.h>

int main(){
  //Assign array elements
  int data[10]={1,2,3,4,5,6,7,8,9,10}; //create array of int
  int index=0;
  //Display all array elements
  for(index;index<10;index++){
    //get element by index value
    printf("data[%d] : %d \n",index,data[index]);
  }
  return 0;
}
C Display 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

Note that in this case both are character array so we can print out all element of array using of format specifiers %s.

Array element are get by index value, But there are possible to written their syntax in following ways.

#include<stdio.h>

int main(){
  //data is an array of 5 integers
  int data[]={10,20,30,40,50,60,70};

  //access array element
  printf("%d\n",data[4]);
  printf("%d\n",*(data+4));
  printf("%d\n",4[data]);
  return 0;
}

Output

50
50
50

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


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved