Home / Questions / How to read and write data in 2-D array?
Explanatory Question

How to read and write data in 2-D array?

👁 534 Views
📘 Detailed Answer
🕒 Easy to Read
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.

Answer with Explanation


#include <stdio.h>

int main()
{
    int a[3][2];
    int i, j;

    // Writing data into 2-D array
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 2; j++) {
            a[i][j] = 2;
        }
    }

    // Reading and printing values
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 2; j++) {
            printf("Value in array: %d\n", a[i][j]);
        }
    }

    // Printing value and address
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 2; j++) {
            printf("Value: %d, Address: %p\n", a[i][j], (void*)&a[i][j]);
        }
    }

    return 0;
}