C/C++  

 

 

 

Array

Array is a kind of data structure that can store multiple data of the same type using the single variable name and index. As you get into the world of programming, array would be one of the data structure that you start using the most frequently. Why ? It is mainly for convinience.

For example, if you want to store 100 integers. Would would get angry if I ask you to create 100 different variable name (e.g, int a1,a2,a3,...,a100). Even C compiler may get angry -:). Obviously it would be much easier if you can define a single variable name and store 100 integers into the variable with using index.

Declaring and Assigning Array

: The most common way to declare an Array is as follows

    data_type   array_name[number_of_elements];

The most common way to assign (store) values to the array is as follows.

    array_name[number_of_elements] = value;

 

Example 01 >

#include <stdio.h>

 

int main()

{

    

    int aryInt[5];

    char aryChar[12];

    int i = 0;

    

    aryInt[0] = 10;

    aryInt[1] = 20;

    aryInt[2] = 30;

    aryInt[3] = 40;

    aryInt[4] = 50;

    

    aryChar[0] = 'H';

    aryChar[1] = 'e';

    aryChar[2] = 'l';

    aryChar[3] = 'l';

    aryChar[4] = 'o';

    aryChar[5] = ' ';

    aryChar[6] = 'W';

    aryChar[7] = 'o';

    aryChar[8] = 'r';

    aryChar[9] = 'l';

    aryChar[10] = 'd';

    

    for(i=0; i < 5; i++){

        

        printf("aryInt[%d] = %d\n", i, aryInt[i]);

    }

    

    printf("\naryChar = ");

    for(i=0; i < 12; i++){

        

        printf("%c",  aryChar[i]);

    }

    

        

    return 1;

}

 

Result :

aryInt[0] = 10

aryInt[1] = 20

aryInt[2] = 30

aryInt[3] = 40

aryInt[4] = 50

 

aryChar = Hello World

Declaring with Initialization

If you know the values to be store in the array when you write the code, you can declare an array and store the value at the same time as shown in following examples.

 

Example 01 >

#include <stdio.h>

 

int main()

{

    

    int aryInt[5] = {10,20,30,40,50};

    char aryChar1[12] = "C-Tutorial";

    char aryChar2[12] = {'H','e','l','l','o',' ','W','o','r','l','d'};

    int i = 0;

    

    for(i=0; i < 5; i++){

        

        printf("aryInt[%d] = %d\n", i, aryInt[i]);

    }

    

    printf("\naryChar1 = %s\n",aryChar1);

    printf("aryChar2 = %s\n",aryChar2);

        

    return 1;

}

 

Result :

aryInt[0] = 10

aryInt[1] = 20

aryInt[2] = 30

aryInt[3] = 40

aryInt[4] = 50

 

aryChar1 = C-Tutorial

aryChar2 = Hello World

Assinging char Array with strcpy, strncpy

In many cases you would find it difficult (cumbersome) to assign the values to array just using the index. If the array is 'char array' there are a couple of handy functions to assign the values to array. It is to use strcpy or strncpy as shown in the following example.

 

Example 01 >

#include <stdio.h>

#include <string.h>

 

int main()

{

    

   // you may simply declaire as 'char aryChar1[12]', but I initialized with "" to fill out

   // null string to all the elements of the array.

    char aryChar1[12] = "";

    char aryChar2[12] = "";

    

    strcpy(aryChar1,"Hello World");

    strncpy(aryChar2,"Hello World",5);

    

    printf("\naryChar1 = %s\n",aryChar1);

    printf("aryChar2 = %s\n",aryChar2);

        

    return 1;

}

 

Result :

aryChar1 = Hello World

aryChar2 = Hello

Passing char Array into a function

Like other data type, you can pass an array to a function as in these examples.

 

Example 01 >

#include <stdio.h>

#include <string.h>

 

void printCharAry(char cAry[255])

{

    

    int i = 0;

    for( i = 0; i < 255; i++)

    {

        if(cAry[i]=='\0') break;

        printf("cAry[%d]=%c\n",i,cAry[i]);

        }   

}

 

int main()

{

    

    char aryChar1[12] = "";

    

    strcpy(aryChar1,"Hello World");

    

    // passing the name of array without []. With this, the address of the first element of

    // the array is passed to the function. By this way, you can pass the full contents of

    // the array to the function.

    printCharAry(aryChar1);

        

    return 1;

}

 

Result :

cAry[0]=H

cAry[1]=e

cAry[2]=l

cAry[3]=l

cAry[4]=o

cAry[5]=

cAry[6]=W

cAry[7]=o

cAry[8]=r

cAry[9]=l

cAry[10]=d

 

Example 02 >

#include <stdio.h>

#include <string.h>

 

void printCharAry(char cAry[255])

{

    

    int i = 0;

    for( i = 0; i < 255; i++)

    {

        if(cAry[i]=='\0') break;

        printf("cAry[%d]=%c\n",i,cAry[i]);

        }   

}

 

int main()

{

    

    char aryChar1[12] = "";

    

    strcpy(aryChar1,"Hello World");

    

    // this is same as printCharAry(aryChar1)

    printCharAry(&aryChar1[0]);

    

    printf("\n");

    

    // passing the name of array with a specific index. With this, you can pass only a portions of the array instead

    // of the whole array into the function.

    printCharAry(&aryChar1[6]);

        

    return 1;

}

 

Result :

cAry[0]=H   // Result of printCharAry(&aryChar1[0]);

cAry[1]=e

cAry[2]=l

cAry[3]=l

cAry[4]=o

cAry[5]=

cAry[6]=W

cAry[7]=o

cAry[8]=r

cAry[9]=l

cAry[10]=d

 

cAry[0]=W  // Result of printCharAry(&aryChar1[6]);

cAry[1]=o

cAry[2]=r

cAry[3]=l

cAry[4]=d

 

Example 03 >

: This example shows the way to define a pointer type argument and pass the array to the pointer argument.

#include <stdio.h>

#include <string.h>

 

void printCharAry(char *cAry,int n)

{

    

    int i = 0;

    for( i = 0; i <n; i++)

    {

        if(cAry[i]=='\0') break;

        printf("cAry[%d]=%c\n",i,cAry[i]);

        }   

}

 

int main()

{

    

    char aryChar1[12] = "";

    

    strcpy(aryChar1,"Hello World");

    

    printf("\nprintCharAry(aryChar1,12)------------\n");

    printCharAry(aryChar1,12);

    

    printf("\nprintCharAry(&aryChar1[0],12)------------\n");

    printCharAry(&aryChar1[0],12);

    

    printf("\nprintCharAry(&aryChar1[6],12)------------\n");

    printCharAry(&aryChar1[6],12);

        

    return 1;

}

 

Result :

printCharAry(aryChar1,12)------------

cAry[0]=H

cAry[1]=e

cAry[2]=l

cAry[3]=l

cAry[4]=o

cAry[5]=

cAry[6]=W

cAry[7]=o

cAry[8]=r

cAry[9]=l

cAry[10]=d

 

printCharAry(&aryChar1[0],12)------------

cAry[0]=H

cAry[1]=e

cAry[2]=l

cAry[3]=l

cAry[4]=o

cAry[5]=

cAry[6]=W

cAry[7]=o

cAry[8]=r

cAry[9]=l

cAry[10]=d

 

printCharAry(&aryChar1[6],12)------------

cAry[0]=W

cAry[1]=o

cAry[2]=r

cAry[3]=l

cAry[4]=d

Returning char Array from a function

There are many different ways to return an array from a function as shown in following examples. Since an Array is assigned as a kind of pointer. If you think of the manipulation of pointer, you can treat the array variable like a pointer when you returning it from a function.

Example 01 >

: Since an array is passed to a faction as a kind of pointer, you can change the contents of the passed array from the function and take the changed array as a return value. Strictly speaking this is not exact function return, but in practical sense you can regard this as a way to return.

#include <stdio.h>

#include <string.h>

 

// cAry[] in this example works both as an input paramter and output parameter.

void toUpper(char cAry[255],int n)

{

    

    int i = 0;

    for( i = 0; i <n; i++)

    {

        if(cAry[i]=='\0') break;

        cAry[i] = toupper(cAry[i]); // change the content and save it to the same array

        }   

}

 

int main()

{

    

    char aryChar1[12] = "";

    

    strcpy(aryChar1,"Hello World");

    

    printf("\nprintCharAry(aryChar1,12)------------\n");

    printf("%s\n",aryChar1);

    

    toUpper(aryChar1,12);

    

    printf("\nprintCharAry(aryChar1,12) after toUpper() ------------\n");

    printf("%s\n",aryChar1);

    

        

    return 1;

}

 

Result :

printCharAry(aryChar1,12)------------

Hello World

 

printCharAry(aryChar1,12) after toUpper() ------------

HELLO WORLD

 

Example 02 >

: In this example, an array will be passed into the function as a pointer and the return value will be saved to the same array as in example 01.

#include <stdio.h>

#include <string.h>

 

// cAry in this example works both as an input paramter and output parameter.

void toUpper(char *cAry,int n)

{

    

    int i = 0;

    for( i = 0; i <n; i++)

    {

        if(cAry[i]=='\0') break;

        cAry[i] = toupper(cAry[i]); // change the content and save it to the same array

        }   

}

 

int main()

{

    char aryChar1[12] = "";

    

    strcpy(aryChar1,"Hello World");

    

    printf("\nprintCharAry(aryChar1,12)------------\n");

    printf("%s\n",aryChar1);

    

    toUpper(aryChar1,12);

    

    printf("\nprintCharAry(aryChar1,12) after toUpper() ------------\n");

    printf("%s\n",aryChar1);

    

        

    return 1;

}

 

Result :

printCharAry(aryChar1,12)------------

Hello World

 

printCharAry(aryChar1,12) after toUpper() ------------

HELLO WORLD

 

Example 03 >

: In this example, the input and output(return) are specified as a separate variable.

#include <stdio.h>

#include <string.h>

 

void toUpper(char *cAryIn,char *cAryOut, int n)

{

    

    int i = 0;

    

    for( i = 0; i <n; i++)

    {

        if(cAryIn[i]=='\0') break;

        cAryOut[i] = toupper(cAryIn[i]);

        }   

}

 

int main()

{

    

    char aryChar1[12] = "";

    char aryCharUpper[12] = "";

    

    strcpy(aryChar1,"Hello World");

    toUpper(aryChar1,aryCharUpper,12);

    

    printf("\nprint aryChar1 ------------\n");

    printf("%s\n",aryChar1);

    

    printf("\nprint aryCharUpper ------------\n");

    printf("%s\n",aryCharUpper);

    

    return 1;

}

 

Result :

pprint aryChar1 ------------

Hello World

 

print aryCharUpper ------------

HELLO WORLD

 

Example 04 >

: Strictly speaking all the above examples cannot be called 'returning an array' even though we can calling 'outputting an array'. I just used 'returning' and 'outputting' interchangeably.

This example would comply to the strict definition of function returning.

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

 

/// the function return type is declared as a pointer

char* toUpper(char *cAryIn, int n)

{

    

    int i = 0;

    char* cAryOut;

    

    cAryOut = (char *)malloc(n); // you need this malloc to assign a block of memory for the

                                                 // output array.

    

    for( i = 0; i <n; i++)

    {

        if(cAryIn[i]=='\0') break;

        cAryOut[i] = toupper(cAryIn[i]);

        }   

        

    return  cAryOut;

}

 

int main()

{

    

    char aryChar1[12] = "";

    char *aryCharUpper;

    

    strcpy(aryChar1,"Hello World");

    aryCharUpper = toUpper(aryChar1,12);

    

    printf("\nprint aryChar1 ------------\n");

    printf("%s\n",aryChar1);

    

    printf("\nprint aryCharUpper ------------\n");

    printf("%s\n",aryCharUpper);

    

        

    return 1;

}

 

Result :

pprint aryChar1 ------------

Hello World

 

print aryCharUpper ------------

HELLO WORLD