What is the correct way to declare a 2D array in C?
Question
What is the correct way to declare a 2D array in C?
Solution
In C, you can declare a 2D array in the following way:
int arrayName[row_size][column_size];
Here's a step-by-step guide:
-
Start with the type of data that will be stored in the array. This could be
int,float,char, etc. For example, let's useint. -
Next, provide a name for the array. This can be any valid variable name. For example, let's use
arrayName. -
After the name, provide the dimensions of the array in square brackets. The first number is the number of rows and the second number is the number of columns. For example, if you want an array with 3 rows and 4 columns, you would write
[3][4]. -
End the line with a semicolon.
So, a 2D array with 3 rows and 4 columns would be declared as follows:
int arrayName[3][4];
This creates a 2D array that can hold 12 integers, arranged in 3 rows and 4 columns.
Similar Questions
How do you initialize a 2D array in C?
What is the correct way to declare and initialize a one-dimensional array in C?
How can you define an array in C/C++?
Which of the following syntax correctly declares an Array in C/C++?int geeks;array geeks[20];geeks{20};int geeks[20];
What is the correct way to declare an array of 10 integers in C?int arr=10;int arr[9];int arr[];int[10] arr;int arr[10];
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.