Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to print all data from an array in C?

I just started with C, i cant find out how to print all values from an array with for loop, wether it is array of strings or integers?

#include <stdio.h>
#include <string.h>

int main()
{

    int array[3][20] = {10, 15, 20};

    for (int i = 0; i < array; i++)
    {
        printf("%d", array[i]);
    }

    return 0;
}

What mistake do i make here?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Since you have a 2-dimensional array, you should use nested loops for each dimension, and access the elements using 2 indexes.

To get the number of elements in an array use sizeof array / sizeof array[0].

for (int i = 0; i < sizeof array / sizeof array[0]; i++) {
    for (int j = 0; j < sizeof array[i] / sizeof array[i][0]; j++) {
        printf("%d ", array[i][j]);
    }
    printf("\n");
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading