What you are doing is printing the value in the array at spot [3][3], which is invalid for a 3by3 array, you need to loop over all the spots and print them.
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
printf("%d ", array[i][j]);
}
printf("\n");
}
This will print it in the following format
10 23 42
1 654 0
40652 22 0
if you want more exact formatting you'll have to change how the printf is formatted.
Answer from twain249 on Stack OverflowWhat you are doing is printing the value in the array at spot [3][3], which is invalid for a 3by3 array, you need to loop over all the spots and print them.
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
printf("%d ", array[i][j]);
}
printf("\n");
}
This will print it in the following format
10 23 42
1 654 0
40652 22 0
if you want more exact formatting you'll have to change how the printf is formatted.
There is no .length property in C. The .length property can only be applied to arrays in object oriented programming (OOP) languages. The .length property is inherited from the object class; the class all other classes & objects inherit from in an OOP language. Also, one would use .length-1 to return the number of the last index in an array; using just the .length will return the total length of the array.
I would suggest something like this:
int index;
int jdex;
for( index = 0; index < (sizeof( my_array ) / sizeof( my_array[0] )); index++){
for( jdex = 0; jdex < (sizeof( my_array ) / sizeof( my_array[0] )); jdex++){
printf( "%d", my_array[index][jdex] );
printf( "\n" );
}
}
The line (sizeof( my_array ) / sizeof( my_array[0] )) will give you the size of the array in question. The sizeof property will return the length in bytes, so one must divide the total size of the array in bytes by how many bytes make up each element, each element takes up 4 bytes because each element is of type int, respectively. The array is of total size 16 bytes and each element is of 4 bytes so 16/4 yields 4 the total number of elements in your array because indexing starts at 0 and not 1.
Printing an array [C]
How to print a full array?
C: print an array - Stack Overflow
c - Is there a way to have printf() properly print out an array (of floats, say)? - Stack Overflow
Videos
I'm trying to print out an array with a set number of rows and variable columns. It can print one and two columns fine but any higher than three, it just shows three total cells.
#include<stdio.h>
int main(void)
{
int N = 0;
int i = 0;
int j = 0;
double container [6][N];
printf("Please enter the number of containers to be entered: ");
scanf("%d", &N);
for (i=0; i<6; i++){
for(j=0; j<N; j++){
container [i][j] = 0;
printf("%lf ", container[i][j]);
}
printf("\n");
}
return 0;
}With 2 as input:
Please enter the number of containers to be entered: 2 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
When higher than 2:
Please enter the number of containers to be entered: 3 0.000000 0.000000 0.000000
In python, I would use a list to store some numbers than print all of them:x = [1, 2, 3, 4]
print(x) #output = [1, 2, 3, 4]
How should I do it in C with an array?
Another question: is an array similar to the python lists? If not, what type would be it?;
That is the memory address of the array (because of array-to-pointer decaying).
To correctly print pointers though, you should use %p.
The "wierd" value you get from printing array is actually the memory address of the first element of the array.
In order to print an array, you need to use a for loop:
for (int i = 0; i < array_size; i++)
printf("%d\n", array[i]);
you need to iterate through the array's elements
float foo[] = {1, 2, 3, 10};
int i;
for (i=0;i < (sizeof (foo) /sizeof (foo[0]));i++) {
printf("%lf\n",foo[i]);
}
or create a function that returns stacked sn printf and then prints it with
printf("%s\n",function_that_makes_pretty_output(foo))
You have to loop through the array and printf() each element:
for(int i=0;i<10;++i) {
printf("%.2f ", foo[i]);
}
printf("\n");