Brand new to C, I come from a Java background.
I am having an issue where I can't compile because the compiler wants to know at compile time the size of my array. For example, I want to print my array to the console. It won't allow me to declare a function prototype as such:
void printRoom(char[][], int, int); //not allowed
What am I supposed to do instead? Is there not a way around this? Online resources that I have found seem to indicate that I MUST know the dimensions if I want to use a function prototype. It appears that it also requires that the function header have the size of the array as well.
void printRoom(char room[][], int height, int width){ // not allowed, missing array bounds
Would a valid solution to this problem just be to say the array is of size 1000*1000 (the maximum array size I can expect)? That seems sloppy to me but I'm pretty sure it would work as long as I stayed within the bounds of what the array size is actually supposed to be.
I am NOT interested in pointers and malloc at this time.
If the compiler supports variable length arrays then you can declare the function the following way
void printRoom( int, int, char[*][*]);
or just
void printRoom( int, int, char[][*]);
Here is a demonstrative program
#include <stdio.h>
#include <string.h>
void printRoom( int, int, char[*][*]);
void printRoom( int m, int n, char a[m][n] )
{
for ( int i = 0; i < m; i++ )
{
printf( "%3s ", a[i] );
putchar( ' ');
}
printf( "\n" );
}
int main(void)
{
const int M = 2;
const int N = 10;
char a[M][N];
strcpy( a[0], "Hello" ),
strcpy( a[1], "World" );
printRoom( M, N, a );
return 0;
}
Its output is
Hello World
If the compiler does not support VLAs then the number of columns has to be a constant. For example
#define N 100
//...
void printRoom(char[][N], int, int);