C Arrays

Arrays are block of contiguous memory location that can store collection of similar type of data items. The data is present in a linear fashion and each data item has an index number.

C arrays start with index 0. The size of the array is the size of the data type times the number of data items. Arrays can be one dimentional or multidimensional.

1D Array Syntax

dataType arrayName[arraySize];

//At initialization
dataType arrayName[3] = {1,2,3};
//or
dataType arrayName[] = {1,2,3};

2D Array Syntax

dataType arrayName[arraySize1][arraySize2];

//At initialization
arrayName[][2] = {1,2,3,4};
//or
arrayName[2][2] = {1,2,3,4};

Integer Array (1D)

To store multiple integers with a single variable name, one has to use an integer array. To create an integer array, an additional size parameter is required along with the variable name in square brackets. Similarly, floating point arrays and other data type arrays can be created.

Example Code

#include<stdio.h>

//g_array of size 10 initialized with 0 by default
int g_array[10];

//g2_array initialised by 1
int g2_array[10] = {1};

int main(void)
{
    //my_array[0] = 1, my_array[1] = 2, ..., my_array[4] = 5
    int my_array[5] = {1,2,3,4,5};
    
    //new_array is unitialized, contains garbage values
    int new_array[100];
    
    int i;
    
    //Finding number of data items present (Avoid hard coding)
    int sizeof_new_array = sizeof(new_array)/sizeof(new_array[0]);
    printf("Size of new_array: %d\n",sizeof_new_array);
    
    for(i = 0;i < sizeof_new_array;i++)
      new_array[i] = i+1;
     
    printf("g_array[0]: %d \n",g_array[0]);
    printf("g2_array[0]: %d \n",g2_array[0]);
    printf("new_array[50]: %d \n",new_array[50]);
    
    return 0;
}

Output

Size of new_array: 100
g_array[0]: 0
g2_array[0]: 1
new_array[50]: 51

Character Array (1D)

In C, one has to use a character array to store strings. Character arrays are ended with a null character '\0' which denotes end of the string. So to store a string of a size n, atleast a character array of n+1 is needed.

Example Code

#include<stdio.h>

//g_array initialized with '\0'
char g_array[5];

int main(void)
{
    //my_array initialised with string "hello"
    char my_array[6] = {'h','e','l','l','o','\0'};
    
    //my2_array initialised with hello, run-time space allocation
    char my2_array[] = "hello";
    
    printf("my_array: %s\n",my_array);
    printf("my_array[0]: %c\n",my_array[0]);
    printf("g_array: %s\n",my_array);
    
    return 0;
}

Output

my_array: hello
my_array[0]: h
g_array:

Integer Array (2D)

Example Code

#include<stdio.h>
int main(void)
{
    int row,col;
    /*
      00,01,02,
      10,11,12,
      20,21,22
    */
    int matrix[3][3] = {1,0,0,
                        0,1,0,
                        0,0,1};
    
    for(row=0;row<3;row++)    //Controls Row
    {
    	for(col=0;col<3;col++)    //Controls Column
    	    printf("%d ",matrix[row][col]);
    	
    	printf("\n");
    }
    
    return 0;
}

Output

1 0 0
0 1 0
0 0 1

Character Array (2D)

Example Code

#include<stdio.h>
int main(void)
{
    int i,j;
    
    //name_db[][100] = {"sam","ron","lana","tim","shawn","sofia"} is valid
    char name_db[6][100] = {"sam","ron","lana","tim","shawn","sofia"};
    
    for(i=0;i<6;i++)    //Controls Column
    	printf("%s\n",name_db[i]);
    
    return 0;
}

Output

sam
ron
lana
tim
shawn
sofia

Trending

C Programming

Remember to follow community guidelines and don't go off topic.