Computer Science

2d Array in C

A 2D array in C is a data structure that stores elements in a grid format with rows and columns. It is created by declaring an array with two sets of square brackets, where the first bracket represents the number of rows and the second bracket represents the number of columns. The elements in the array can be accessed using their row and column indices.

Written by Perlego with AI-assistance

1 Key excerpts on "2d Array in C"

  • C Programming
    eBook - ePub

    C Programming

    Learn to Code

    a multidimensional array. In this section, we will discuss the 2D array.
    In the 2D array, we require two subscripts to indicate one element. One subscript denotes the row number and the other subscript denotes the column number. Like 1D arrays, the subscript value starts from 0 (zero). Figure 10.11 shows an example to denote an element in a 2D array.
    Figure 10.11 An element of a 2D array with two subscripts.

    10.5.1 Introducing Matrices

    A matrix mat[m][n] is an m by n table having m rows and n columns containing m × n elements (refer to Figure 10.12 ). Each element of the matrix is represented by mat[i][j].
    Figure 10.12 A matrix.
    where,
    i represents the row number and varies from i = 0, 1, 2, …, m − 1
    j represents the column number and varies from j = 0, 1, 2, …, n − 1

    10.5.2 Declaration of a 2D Array

    We use a general form shown in (Figure 10.13 ) to declare a 2D array.
    Figure 10.13 Syntax of a 2D array declaration and example.
    The example code (Figure 10.13b) will create an array of integers named Mat and can store 4 x 3 = 12 elements of integer type. The element of the array can be accessed by Mat[0][0], Mat[0][1]. . . . . Mat[2][3] and so on. According to its characteristics, all the elements of the matrix will be stored in contiguous memory locations.

    10.5.3 Representation of a 2D Array in Memory

    Since we know that array elements will be stored in contiguous memory locations, the 2D array is also stored in contiguous memory locations. In memory, whether a 1D or a 2D array, the array elements are stored in one continuous chain.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.