"Herbert Rosenau" <os2guy@pc-rosenau.dewrites:
[...]
Quote:
struct X *f(struct X *p);
>
Please tell me what f() gets and what if gives back;
It gets a pointer to struct X. It gives back a pointer to struct X.
Quote:
You can't distinguish between
>
f gets called with a pointer to a single struct
f gets called with an array of structs with an unknown number of
elements
Strictly speaking, the former is possible in C, and the latter is not.
You cannot directly pass an array as a function argument.
More loosely, you're right, of course. Given just the declaration
you've shown us, there's no way to tell whether the argument passed to
f was a pointer to a single struct or a pointer to the first element
of an array of structs.
If it's intended that the argument is going to be a pointer to an
array, then the function should have some mechanism for the caller to
tell it how many elements the array has. The language doesn't provide
a good way to tightly associate this information with the pointer
declaration, so it's generally done via program logic:
struct X *f(struct X *p, size_t len);
Quote:
And you can't distinguish too what f() returns. Returns it a pointer
to a single struct or a pointer to an array of structs?
It returns a pointer to a struct. It does not return a pointer to an
array. If it did, its declaration would be quite different. If I'm not
mistaken, it would be something like this:
struct X (*f(int (*p)[LEN]))[LEN]
where LEN needs to be a constant expression. Note that I've changed
both the argument type and the return type. Judicious use of typedefs
might make this more legible.
C actually does have pointers to arrays. However, pointers to arrays
are rarely used to manipulate arrays (other than implicitly in
multidimensional array, which are themselves relatively rare).
Instead, arrays are usually manipulated via a pointer to the first
element of the array *which is of a distinct type*.
Quote:
In C89 you don't have VLA, so you can't not even give a hint other
than in docomuntation or reading the source of f() carefully.
Right.
Note that I didn't quote any of my previous article, the one to which
you were replying. This is because, as far as I can tell, you didn't
actually dispute or otherwise address anything I wrote there.
I invite you to go back and do so. If I've gotten something wrong,
I'd love to hear about it.
--
Keith Thompson (The_Other_Keith)
kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"