Hello there!

I wrote a small program in C on Windows using DevC++. It works perfectly, but when I try to run it on Linux I get compiler erros at these lines:

void printOctets(int octets[][], char []); 
void printInterval(int suntwk[][], int brdc[][]);

This is a college assignment and I'm supposed to write this for Linux.

I'm lost, don't know what to do.

Any hints??

Dani AI

Generated

Short, practical clarification and a few safe patterns for passing 2-D arrays in C (Linux/gcc), tied to the thread: was right that leaving both dimensions blank is illegal, and 's report that "specifying the dimensions fixed it" is exactly what the compiler needs. The rule is that when an array is declared as a function parameter the compiler must know the size of every dimension except the first so it can compute element addresses.

Correct, commonly used patterns:

/* fixed column count (macro or constant) */
#define COLS 8
void print_octets(int arr[][COLS], size_t rows);

/* pointer to an array of COLS ints (same layout as above) */
void print_octets_ptr(int (*arr)[COLS], size_t rows);

/* C99 variable-length arrays (sizes supplied before the VLA parameter) */
void print_vla(size_t rows, size_t cols, int arr[rows][cols]);

Notes and pitfalls: the leftmost size in a parameter like int a[10][20] is ignored; the compiler only needs the later dimensions. int ** is not equivalent to int a[][COLS] — a pointer-to-pointer has different memory layout unless rows were allocated and arranged to match. Alternatives when the column count cannot be constant: pass the shape explicitly and either use a VLA (C99+) or pass a flattened 1-D buffer and index with arr[i*cols + j]. If using VLAs, compile with a C99/C11 mode (for example -std=c99 or -std=gnu11).

For authoritative language rules and examples see the C arrays documentation: .

Recommended Answers

All 2 Replies

It will help to know what the error messages are. I'm supprised those compiled with a MSWindows compiler. You can't leave both dimensions of the arrays unspecified

void printOctets(int octets[10][], char []); 
void printInterval(int suntwk[10][], int brdc[10][]);

Indeed!
Solved the problem.
I specified the array dimensions and things worked perfeclty.
This is what happens when you stop working with a language for some time and delve into another one.
Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.