Changing Multidimensional Arrays

Hi! im learning about Multidimensional Arrays right now and my head is stuck with the thought of implement a whole Row after create the Array. In my example i got a 2 x 3 integer Array called “matrix” so first i create the first row with 3 columns

#include<stdio.h>

int main() {
int matrix[2][3] = {2, 6, 9};

so in my head the first row is 2, 6, 9. After creating this i want to add a whole row like this:

matrix[1][] = {7, 2, 4};

so the second row should be 7, 2, 4.

So in total the Array should look like this:
2 6 9
7 2 4
i think the syntax is completely wrong to do what is in my head and explained.

i hope some people out here understanding my thoughts to help me with that dilemma.

When creating a two-dimensional int array, you’re basically creating an array that contains integer arrays. That means you end up with something that looks like this:

{{1,2},{3,4},{5,6}}

Notice how the smaller arrays (e.g. {1,2}) are contained within the larger array?


When declaring and initialising a two-dimensional array, the format you have used is correct. The first number refers to the length (number of elements) of the outer array, and the second number refers to the number of elements that can be contained within each nested array:

int matrix[2][3] = {{1,2,3},{4,5,6}};
                       ^           ^
                       |            \
                 nested array       closing brace of outer array
//there can be up to two nested arrays (because of the [2])
//each nested array can contain three elements (because of the [3])

This article provides a more in-depth explanation as well.

I hope this helps!

1 Like

thanks for ur quick answer!
i already understand the fundamentals of Multidimensional Arrays. My exact question is if i can add an inner array after created the 2 x 3 array. At first i want to implement ONLY the first inner array :

int matrix[2][3] = {1, 2, 3};

in this array i have space for a second row or inner array. and i want to know how i can add this second row later in my code.

1 Like

Ah; sorry for the misunderstanding!


You should be able to do something like this, after initialising the array with just the first element:

int matrix[2][3] = {1,2,3};
//a bunch of random lines
matrix[1][0] = 1;
matrix[1][1] = 2;
matrix[1][2] = 3;

That’s the best solution I can find, but it’s pretty clunky…

1 Like