How to create and use 2D arrays in C

Arrays make it possible to store a set of related data in C without having to create multiple variables. As a rule, arrays are one-dimensional, but they can be expanded to include any number of dimensions. We’ll show you how to create 2D arrays in C and how to use them effectively.

What are the basics of creating arrays in C?

To be able to create and master 2D arrays in C, you need to know the basics of this structure in the C programming language.

Similar to many other languages, arrays in C are a memory block in which several values of the same data type can be stored. This allows several values to be saved and combined under one variable name. The size of the array must be known at compile time and cannot be changed afterwards. The following code block illustrates how to create arrays in C:

int numbers1[15];
// Optionally, the saved values can be specified when they are created:
int numbers2[5] = {1, 2, 3, 4, 5};
c

The data type is specified in front of the variable name. In this example, the square brackets indicate that this variable is not a single value, but an array. The integer between the brackets indicates how many elements of this data type can be stored in the array. In the above example, the array number1 is created uninitialised. This means that no values are written in the fields. These can then be filled with values in the code later on.

The array number2, on the other hand, is initialised manually when created. This approach is time-consuming for larger arrays and the resulting code is often unintelligible. So, in most cases, it’s not the best approach. It’s usually better to try to fill your arrays programmatically. You can do this, for example, with a for-loop:

int numbers3[100];
for(int i = 0; i < 100; i++) {
numbers3[i] = i + 1;
}
// Creates an array containing the integers 1 to 100.
c
Tip

Depending on which operating system you have, which C standard you’re using, and where you’ve declared an uninitialised variable in your program, this variable may contain a random value. This applies to array fields as well. This is why it’s best not to access fields that you haven’t initialised yet, particularly if the value saved in them is interpreted as a pointer.

Once an array has been created, you can use its index to access individual values. It’s important to note that arrays start with the index 0. The example below will show you:

int numbers2[5] = {1, 2, 3, 4, 5};
numbers2[3] = numbers2[2];
printf("%d\n", numbers2[3]);
// Output: 3
c

A 2D array can be carried out in C by creating an array in which each field contains another array. You can read more about 2D arrays in the next section.

How to create 2D arrays in C

2D arrays in C are really just one-dimensional arrays. Each field just contains another array. A 2D array can be understood as a table or matrix of values. 2D arrays can be created and filled with the following syntax:

ints_two_dimensions[10][10];
ints_two_dimensions [0][1] = 0;
ints_two_dimensions [2][1] = 2;
ints_two_dimensions [9][4] = 36;
// etc.
c

Here the number in the left square brackets represents the index to be accessed in the first array. The number on the right represents the index in the second. Think of these two numbers like 2D coordinates, or row or column numbers. Just like one-dimensional arrays, two-dimensional arrays can also be initialised with values when they are created.

floats_two_dimensions[2][6] = {
{0.1, 3.56, 6.346, 8.9, 45.345, 2.284},
{7.0, 1.12, 9.74, 0.0, 3.56, 4.4}
}
c

The principle of an array within another array is not limited to two dimensions. You can use this method to create arrays with any number of dimensions.

int ints_four_dimensions[10][10][10][10];
c
Web Hosting
Secure, reliable hosting for your website
  • 99.9% uptime and super-fast loading
  • Advanced security features
  • Domain and email included

How you can use 2D arrays in C

Iterate over a 2D array

2D arrays in C (or multidimensional arrays) are most commonly used to create multidimensional data structures. In the following example, a two-dimensional array is filled alternately with zeros and ones to represent a chessboard:

#include <stdio.h>
#define ARRAY_LENGTH 8
int main() {
    int chessboard[8][8];
    for(int i = 0; i < ARRAY_LENGTH; i++) {
        for(int j = 0; j < ARRAY_LENGTH; j++) {
            chessboard[i][j] = (i + j) % 2;
            printf("%d", chessboard[i][j]);
        }
        printf("\n");
    }
    return 0;
}
/*
Output:
01010101
10101010
01010101
10101010
01010101
10101010
01010101
10101010
*/
c

String arrays

If you want to use arrays skillfully, it’s important to keep in mind that an array is just a pointer to a location in memory and this is how the C compiler understands it. The index specified when writing on or reading from individual fields represents the shift along the array relative to the base address. Let’s take a look at the following example to get a better idea:

#include <stdio.h>
int main() {
int number2[5] = {1, 2, 3, 4, 5};
printf("%d\n", *number2);
// Output: 1
printf("%d\n", *(number2 + 2));
// Output: 3
}
c

Strings are treated in the same way in C. It’s possible to iterate over a string as if it were an array. You can see this in the following example where three records stored in an array are written character by character in capital letters. To access characters, the array index field (square brackets) is used:

#include <stdio.h>
int main() {
    char* sentences[3];
    sentences[0] = "Hello, this is the first sentence.\n";
    sentences[1] = "This is the second sentence.\n";
    sentences[2] = "And now there are three.\n";
    printf("Original sentences:\n\n");
    for(int i = 0; i < 3; i++) {
        printf("%s", sentences[i]);
    }
    printf("\nChanged sentences:\n\n");
    for(int i = 0; i < 3; i++) {
        int j = 0;
        while(sentences[i][j] != '\n') {
            if(sentences[i][j] >= 'a' && sentences[i][j] <= 'z') {
                printf("%c", sentences[i][j] - 0x20);
            } else {
                printf("%c", sentences[i][j]);
            }
            j++;
        }
        printf("\n");
    }
}
/*
Output:
Original sentences:
Hello, this is the first sentence.
This is the second sentence.
And now there are three.
Changed sentences:
HELLO, THIS IS THE FIRST SENTENCE.
THIS IS THE SECOND SENTENCE.
AND NOW THERE ARE THREE.
*/
c
Was this article helpful?
Page top