Home My Page Projects Modern C
Summary Activity Forums Tracker News SCM Files

Forum: discussion

Monitor Forum | Start New Thread Start New Thread
RE: Forward declaration with array notation [ Reply ]
By: Jens Gustedt on 2020-07-13 21:15
[forum:153519]
Effectively, that seems to be a pitfall if the structure type is not yet known at the point of declaration of the function. I hadn't though of this.

So effectively this reduces the usefulness of the `static` in the array parameters even more.

It is really time that C switches to attributes for that kind of notations.

Thanks for pointing this out

Jens

Forward declaration with array notation [ Reply ]
By: Some Guy on 2020-07-13 18:36
[forum:153516]
Dear Jens Gustedt,

I'm currently implementing a toy C(18) library to exercise with the notion of your book, but I'm struggling trying to use forward declarations with array notation.
Suppose I want to define a type, let's say `List`, in an header file using forward declaration, so I do this

File List.h: the interface
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...
typedef struct List List;
...
unsigned listLength(List const self[static 1]);
...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

File List.c: the implementation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...
struct List {
unsigned len;
...
};
...
unsigned listLength(List const self[static 1]) {
...
return self->len;
}
...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

When I try to compile this in a static library, the compiler shows me this error message:
$: [...] error: array type has incomplete element type ‘List’ [...]
unsigned listLength(List const self[static 1]);
^~~~~~
I'm currently using the last version of GCC.

Is my usage correct, or must I use the pointer version (which works absolutely fine)?