Notes on the array data type in C

Synopsis

[data type] arrayName[array size] = {default value}

Initialisation

To create an array that can hold up to 10 (integer value) key value pairs (with the keys 0 through 9):

int myNumbers[10];

Assigning values:

int myNumbers[0] = 5;
myNumbers[1] = -3;
...
myNumbers[9] = 7;

Or you can use the following braces syntax for the declaration and value assignment on a single line.

int myNumbers[10] = {5, -3, 2, 7, -6, 1, 6, -3, 2, 7};

If you wish to set the value of specific keys only using braces, you can do so like this:

int myNumbers[10] = { [3] = 7, [8] = 2, [9] = 7 };

This sets the values of entries 3, 8 and 9.

In C unspecified values are usually set to zero (but aren’t always if there happens to be junk in that memory location). For example:

int otherNumbers[10];

otherNumbers[0] = 1;
otherNumbers[1] = 2;

might not necessarily mean that keys 2 through 9 have a value of zero (0). So it’s good practice when creating an array to explicitly set the values you want. With C++ there is a helper method to set the default value of an entire array. However, there isn’t such a luxury in C.

In C you can omit the size of the array (also known as the array dimension). E.g:

int numbers[] = {1, 2, 3, 4, 5};

This approach is fine so long as you initialise every element in the array at the point that the array is defined. If this is not the case, you must explicitly define the dimension like so:

int numbers[5];

Below are examples of a floating point arrays:

float realNums[3];
realNums[0] = 0.2;
realNums[1] = -4.;
realNums[2] = 6.7219;

double realNums2[3];
realNums2[0] = 0.2;
realNums2[1] = -4.;
realNums2[2] = 6.7219;

Below is an example of a char array. Single char arrays must use single quotes within the value assignments:

char myName[12] = {'J', 'o', 'n', ' ', 'M', 'a', 't', 't', 'h', 'e', 'w', 's'};
    
for (int i = 0; i < 12; i++) {
    printf("%c", myName[i]);
}

Enumeration

short unsigned int i;
for (i=0; i<10; i++) {
    printf("%i = %i\n", i, myNumbers[i]);
}

Arrays in functions

When you pass an *entire* array to a function, any modifications to that array modify the original array. As in, the parameter's array declaration isn't a copy of the original, but a reference to it. See example below:

void doubleScores(float scoreArray[], unsigned short int arraySize)
{
    unsigned short int i;
    for (i = 0; i < arraySize; i++) {
        scoreArray[i] = (scoreArray[i] * 2);
    }
}

int main(int argc, const char * argv[])
{
    
    float scores[3] = {0.f};
    
    scores[0] = 7.f;
    scores[1] = 6.33;
    scores[2] = 1.27;
    
    unsigned short int i;
    
    for (i=0; i<3; i++) {
        printf("score %i is %f\n", i, scores[i]);
    }
    
    printf("----------------\n");
    doubleScores(scores, 3);
    
    for (i=0; i<3; i++) {
        printf("Doubled score %i is %f\n", i, scores[i]);
    }
    
    return 0;
}

Output:

score 0 is 7.000000
score 1 is 6.330000
score 2 is 1.270000
----------------
Doubled score 0 is 14.000000
Doubled score 1 is 12.660000
Doubled score 2 is 2.540000

However, parameterised individual array elements arr[x] are passed as copies, not references (just like normal C primitives int, float, char etc).

void doubleScore(float singleScore)
{
    singleScore = (singleScore * 2);
}

int main(int argc, const char * argv[])
{
    
    float scores[3] = {0.f};
    
    scores[0] = 7.f;
    scores[1] = 6.33;
    scores[2] = 1.27;
    
    printf("Score 2 is is %f\n", scores[2]);
    
    doubleScore(scores[2]);
    
    printf("Score 2 is is %f\n", scores[2]);
    
    return 0;
}

Output:

Score 2 is 1.270000
Score 2 is 1.270000

Multidimensional Arrays

int array2D[number_of_rows][number_of_columns];

Here is the initialisation of a multidimensional array with 2 rows and 3 columns:

int array2D[2][3] =
{
{ 2, 7, 1 }, // row 1 (3 columns in each)
{ 9, 4, 2 }  // row 2 (3 columns in each)
};

This array can be depicted like so:

- Column 1 Column 2 Column 3
Row 1 2 7 1
Row 2 9 4 2

Items are accessed like so:

- Column 1 Column 2 Column 3
Row 1 array2D[0][0] array2D[0][1] array2D[0][2]
Row 2 array2D[1][0] array2D[1][1] array2D[1][2]

Example of a three dimensional array:

int array3D[2][3][2] =
    {
        { { 2, 6 }, { 7, 3 }, { 1, 5 } },
        { { 9, 8 }, { 4, 4 }, { 2, 1 } }
    };

Leave a Reply

Your email address will not be published. Required fields are marked *