Keith Thompson said:
Bill Cunningham said:
§1.9 page 29 has this function.
int getline (char s[], int lim)
Unless I'm missing something here to pass an array shoudn't that first
parameter be char *s ?
It depends on what you mean by "should".
The rest of this thread indicates that you're aware that this:
int getline (char s[], int lim)
and this:
int getline (char *s, int lim)
are equivalent. This is implied by N1370 6.7.6.3 paragraph 7, or by the
corresponding paragraph in earlier versions or drafts of the C standard.
There *is* something funny about char[] and char* being equivalent here.
It's apparent when doing automatic translators of type specifications (from
English, say) to C.
Translating 'array of char' to C is just 'char[]' (0 indirections) for an
ordinary declaration.
And 'pointer to array of char' is 'char(*)[]' (1 indirection). There's a
natural progression.
Now try and use these translations for function parameters; there's an
off-by-one error! A char(*)[] will give two indirections, while char[] gives
you one.
I suppose that since a true char[] parameter would allow pass-by-value
arrays, it was decided to make it mean something else. But it might have
been more useful to actually let it mean that, and to raise an error if
someone tried to use it without adding something more to it (such as
char(*)[]).
It would have made automatic translators simpler too. (Actually there was
bug in my translator because of this. It still worked though, because so
many casts were used elsewhere to make things right; I could have used float
instead of char* or char[] and it would have worked..)