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?
CodePudding user response:
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");
}