Quote:
>I need to write a function with the following prototype:
>int OrcaPluginProc(pblock *Config,Message **ppInMsg,Message ***pppOutMsg)
>so pppOutMsg is a pointer to a pointer to a pointer to a
>Message struct.
It is not really clear what all this is for, but I can suggest one
place where someone might want such a thing. Imagine you have a
function looking like this:
int main(int argc, char **argv) {
... code that uses argv[i] for some i ...
}
Of course such an interface is terribly rare :-) so no one will ever
have seen it before or be familiar with it. :-)
Silliness aside, most C programmers should be quite familiar with
the above. But suppose that, from your main() function, you decide
to call a new function that can supply a new "argv" and "argc" pair.
How will you change argc and argv in the target function? The answer,
of course, is to receive pointers to them:
/* who knows what this returns */
double maybe_change_them(int *pargc, char ***pargv) {
... code that maybe changes argc and argv ...
}
In main(), you simply call this as:
val = maybe_change_them(&argc, &argv);
and upon its return you may have new values in argc and argv.
What about "maybe_change_them" itself? Well, suppose it decides
not to change argc and argv. This is easy: just return without
changing them. But what if you do want to change argc and argv?
Well, suppose the new argc is always exactly 3, and the new argv
is always the set {"prog", "-x", "42", NULL}. Then:
double maybe_change_them(int *pargc, char ***pargv) {
static char *newav[4] = { "prog", "-x", "4", NULL };
...
*pargc = 4;
*pargv = newav;
...
return 3.14159;
}
What have you allocated and produced here?
The answer is: you allocated the array "newav" of size 4 of "char
*"s, and for each newav[i], you provided a value. The value you
provided points to the first char in an an array of "char"s, where
each of those arrays has a different size: one is size 5, one size
3, and one size 2. The last element, newav[3], you set to NULL.
The "array 5 of char" is initialized with {'p', 'r', 'o', 'g', '\0'};
the "array 3 of char" is initialized with {'-', 'x', '\0'}; and the
"array 2 of char" is initialized with {'4', '\0'}.
Each of your "allocations" is static and is actually performed by
the compiler. You could have spelled out the three "array N of
char"s explicitly:
static char av0[] = {'p', 'r', 'o', 'g', '\0'};
static char av1[] = "-x"; /* same as {'-', 'x', '\0' } */
static char av2[] = "4";
static char *newav[4] = { av0, av1, av2, NULL };
Or, you could use malloc() or equivalent to allocate "newav" and
each "avX" dynamically:
char **newav;
...
newav = malloc(N * sizeof *newav);
if (newav == NULL) ... handle "out of memory" error ...
for (i = 0; i < N; i++)
newav[i] = some_string(); /* might need another malloc() */
Thus, given the type signature above (repeated here):
Quote:
>int OrcaPluginProc(pblock *Config,Message **ppInMsg,Message ***pppOutMsg)
I suspect that "OrcaPluginProc" is really supposed to *set* a
variable of type "Message **", just like maybe_change_them() *sets*
argv, a variable of type "char **". In order to set it, it needs
that variable's address, which is a value of type "Message ***".
If the "incoming message" (ppInMsg) happens to be valid as the
"outgoing message", the assignment:
*pppOutMsg = InMsg;
would do the trick. Otherwise you may need to create an array
dynamically, or use a statically-allocated single array:
static Message *fixedmsg[] = { some, values };
or:
Message **newmsg;
...
newmsg = malloc(N * sizeof *newmsg);
if (newmsg == NULL) ... handle error ...
for (i = 0; i < N; i++)
newmsg[i] = some_msg();
--
In-Real-Life: Chris Torek, Wind River Systems