C Multidimensional Arrays

1. What is a Multidimensional Array?

  • A multidimensional array is an array of arrays.

  • The most common is a 2D array, which can be thought of as a table with rows and columns.

Syntax for 2D Array:



 

  • rows → number of rows

  • columns → number of columns


2. Declaring and Initializing a 2D Array

Declaration Only



 


Declaration with Initialization

 

  • Can also initialize in a single line:



 


3. Accessing Elements in a 2D Array

  • Use row and column indices (both start from 0):


 

Output:

Element at row 1, column 2: 20
Element at row 2, column 3: 60

4. Looping Through a 2D Array


 

Output:

1 2 3
4 5 6

5. Sum of All Elements in 2D Array


 

Output:

Sum of all elements: 21

6. Real-Life Example: Student Marks Table


 

Output:

Student 1 marks: 85 90 78
Student 2 marks: 88 76 92
Student 3 marks: 90 85 88

Real-life use: Class marks, Excel-like tables, seating arrangements.


7. Key Points About Multidimensional Arrays

  1. 2D arrays = rows × columns; 3D arrays = tables of matrices.

  2. Use nested loops to access all elements.

  3. Memory is contiguous, so indexing is efficient.

  4. Can initialize row-wise or single line.

  5. Avoid accessing indices outside the declared size → undefined behavior.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

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