I have the following code :
#include <stdio.h>
void mostrarMatriz(int, char[][]);
void main(int argc, char* args[]){
mostrarMatriz(argc, args);
}
void mostrarMatriz(int filas, char matriz[][]){
for(int i=0; i<filas; i++){
int columnas = sizeof(matriz[i])/sizeof(char);
for(int j=0; j<columnas; j++){
printf("%c\t", matriz[i][j]);
}
printf("\n");
}
}
And I get the following errors :
From what I know of C, I must pre-declare the function. This is declared, but it doesn't recognize the character array declaration. I do not understand why it is or if the rest of the errors are a consequence of this erroneous declaration.
The goal of the program is to output the program call arguments to the screen. For example, assuming the program is called Blue, we have:
$Azul Hola 34
A z u l
H o l a
3 4
Thanks for your attention.
Take the statement:
This is an array, to which you do not indicate the size, and whose elements are of type
char[]
. The problem is that itchar[]
is an incomplete type. According to the standard, if the size of the array is not indicated, it is an incomplete type, and the type of the elements of an array cannot be of an incomplete type. If you want a more detailed explanation, I recommend you to see this question .One solution could be to make it a full type by giving the array a size, but this doesn't seem to be possible since the amounts depend on user input, one that we don't have available at compile time.
The other option would be to take the parameter exactly the same as you take it in the
main
:Note: The correct way to take the size is not:
This only works if the size of the array is known at compile time. The correct way to get the size would be using
strlen
.The corrected code would be:
In C you have to declare the size of the array. In case of being two-dimensional you can declare it in the following way: