Hello,
I'm confused. With C, when calling a function, all argument
(parameter) passing is is by value. That's how C is defined.
True. By that logic, nothing in C can ever be passed by reference.
However, I don't think it's unreasonable to refer to passing the address
of a variable as "passing by reference". That's the normal way of
passing things by reference in the C language.
So what's wrong with "C-structs are passed that way"? For it not to
be passed "that way" (by value) you need to change what you're passing --
namely to a pointer to the struct.
That's a technicality, Bruce, and you know it -- that's why you refer to
your message as "nit picking".
What's wrong with the original statement of "C-structs ar passed that
way" is that it implies that whenever a C programmer wants to pass a
data structure from one function to another, that they *always* pass it
by value. And that's simply not true. Heck, it's not even true in a
majority of cases.
You almost always see structures passed like this:
struct mydata {
int var1;
char var2[50];
};
int dosomething(struct mydata *arg) {
/* whatever */
}
int main(int argc, char **argv) {
struct mydata x;
if (dosomething(&x)) {
/* whatever */
endif;
return 0;
}
You can say "but, when you pass a pointer, you're not passing the data
structure, you're passing a pointer" but that's just a technicality.
The programmer's intent is to pass a data structure by reference. That's
the *reason* why this is being done, and it's by far the most common
means of passing a data structure in C (at least in my experience).
Vern's post was aimed at an RPG programmer trying to call a function
that was written in C, and documented by IBM using C prototypes and C
examples instead of the more common language-neutral API documentation
format.
The RPG programmer was trying to figure out how to call the function.
And in this case, he *should* pass it by value. But this is not because
"C always passes data structures by value". If you called another
function where it passed the data by address, then you should pass it by
address. Do what the function calls for, and NOT "always by value
because that's the way C does it."
As an Amazon Associate we earn from qualifying purchases.