C Arrays

Introduction

Arrays are data structures consisting of related data items of the same type. Arrays are static structures and remain the same size throughout program execution.

Arrays

An array is a group of memory locations related by the fact that they all have the same name
and the same type. To refer to a particular location or element in the array, we specify the
name of the array and the position number of the particular element in the array.

In the following example the array c contains 12 elements (0…11). The first element in every
array is the zeroth element. So the first element is the zeroth element. The position number contained within square brackets is more formally called a subscript. A subscript must be an integer or an integer expression.

Example 1

int c[12];

Example 2

int main(int argc, char *argv[])
{
 
    /* initializing an array */
    int array[12];
    int i;
    for(i = 0; i<12; ++i)
    {
          array[i] = 0;
    }  
   /*
      A second way of initializing an array is the following:
     int array[12] = {0,0,0,0,0,0,0,0,0,0,0,0};
  */
 
    printf( "%s%13s\n", "Element", "Value" );
    for(i = 0; i<12; ++i)
    {
          printf( "%7d%13d\n", i, array[ i ] );
    }  
 getchar();   
}

Example 3
Compute the sum of the elements of an array

int main(int argc, char *argv[])
{
 
    int a[ SIZE ] = { 1, 3, 5, 4, 7, 2, 99,16, 45, 67, 89, 45 };
    int i = 0;
    int total = 0;
    printf( "%s%13s\n", "Element", "Value" );
    for(i = 0; i<SIZE; ++i)
    {
          total+=a[i];
    }  
 
    printf( "Total of array element values is %d\n", total );
}

Example 4
Forty students were asked to rate the quality of the food in the student cafeteria on a scale
of 1 to 10 (1 means awful and 10 means excellent). Place the 40 responses in an integer
array and summarize the results of the poll.

/* Student poll program */
#include <stdio.h>
#define RESPONSE_SIZE 40
#define FREQUENCY_SIZE 11
 
int main()
{
 
 int answer, rating, frequency[ FREQUENCY_SIZE ] = { 0 };
 int responses[ RESPONSE_SIZE ] =
                { 1, 2, 6, 4, 8, 5, 9, 7, 8, 10,
                  1, 6, 3, 8, 6, 10, 3, 8, 2, 7,
                  6, 5, 7, 6, 8, 6, 7, 5, 6, 6,
                  5, 6, 7, 5, 6, 4, 8, 6, 8, 10 };
 
 for ( answer = 0; answer <= RESPONSE_SIZE - 1; answer++ )
     ++frequency[ responses [ answer ] ];
 
 printf( "%s%17s\n", "Rating", "Frequency" );
 
 for ( rating = 1; rating <= FREQUENCY_SIZE - 1; rating++ )
  printf( "%6d%17d\n", rating, frequency[ rating ] );
 
 return 0;
}

Example 5

/*
This program reads numbers from an array and graphs the information
in the form of a bar chart or histogram—each number is printed, 
then a bar consisting of that many asterisks is printed beside the number.
*/
#include <stdio.h>
#define SIZE 10
int main()
{
 int n[ SIZE ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 };

 int i, j;
 printf( "%s%13s%17s\n", "Element", "Value", "Histogram" );
 for(i = 0; i< SIZE; ++i)
 {
         printf( "%7d%13d ", i, n[ i ]) ;

         for ( j = 1; j <= n[ i ]; j++ ) /* print one bar */
             printf( "%c", '*' );

         printf( "\n" );

 }                

return 0;
}

Character Arrays

A string is a static array of individual characters in C.

A character array can be initialized using a string literal.

Example 6
The declaration

char string1[] = "first";
//string2 is the same
char string2[] = { 'f', 'i', 'r', 's', 't', '\0' };

Initializes the elements of array string1 to the individual characters in the string literal "first". The size of array string1 is five characters plus a special string termination character called the null character '\0'

Passing Arrays to Functions

To pass an array argument to a function, specify the name of the array without any brackets. When passing an array to a function, the array size is often passed so the function can process the specific number of elements in the array.

C automatically passes arrays to functions using simulated call by reference —the called functions can modify the element values in the callers’ original arrays. The name of
the array is actually the address of the first element of the array! Because the starting address of the array is passed, the called function knows precisely where the array is stored.
Therefore, when the called function modifies array elements in its function body, it is modifying
the actual elements of the array in their original memory locations.

Passing arrays simulated call by reference makes sense for performance reasons. If arrays
were passed call by value, a copy of each element would be passed. For large, frequently
passed arrays, this would be time consuming and would consume considerable storage for
the copies of the arrays.

Example 7
Program which demonstrates the difference between passing an entire array and passing
an array element.

#include <stdio.h>
#define SIZE 5
 
void modifyArray( int [], int );
void modifyElement( int );
int main()
{
    int a[ SIZE ] = { 0, 1, 2, 3, 4 }, i;
 
    printf( "Effects of passing entire array call "
            "by reference:\n\nThe values of the "
            "original array are:\n" );
    for ( i = 0; i <SIZE; i++ )
          printf( "%3d", a[ i ] );
 
    printf( "\n" );
 
    modifyArray( a, SIZE ); /* passed call by reference */
    printf( "The values of the modified array are:\n" );
 
     for ( i = 0; i <SIZE; i++ )
        printf( "%3d", a[ i ] );
 
     printf( "\n\n\nEffects of passing array element call "
        "by value:\n\nThe value of a[3] is %d\n", a[ 3 ] );
    modifyElement( a[ 3 ] );
    printf( "The value of a[ 3 ] is %d\n", a[ 3 ] );
 
    getchar();
    return 0;
}
 
void modifyArray(int a[], int size)
{
     int k;
 
     for(k = 0; k<size; ++k)
           a[k] = a[k]+1;
 
}
 
void modifyElement(int i)
{
     printf( "Value in modifyElement is %d\n", i *= 2 );
}

Sorting Arrays

More about sorting data can be found in the sorting arrays section.

Searching Arrays

More about searching data can be found in the searching arrays section.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.