[lug] C programming Q.
rm at fabula.de
rm at fabula.de
Wed Apr 17 11:05:55 MDT 2002
On Wed, Apr 17, 2002 at 11:54:41AM -0500, Michael J. Hammel wrote:
> I hate this. We're using some gawd-awful args to gcc that force all
> warnings into errors so compiles fail. Ick.
No, this is a _good_ habbit, really ;-) Since i made this the default
in my projects (and cranked up the warning switches) i had way less
bugs sneak into my production code.
> I've cleaned up most of them,
> but now I've hit one that I've never hit before.
>
> We have a struct that has a function pointer as a member, such as:
>
> typedef struct teststruct{
> char *name;
> int (*fp) (void);
> int test_type;
> } teststruct_0;
>
> An initialization using this struct looks like this:
>
> teststruct_0 tests_noarg[] =
> {
> {"Stuck Address", test_stuck_address, someval}
> }
>
> The function "test_stuck_address" has this prototype:
>
> int test_stuck_address (uint *val1, uint *val2, uint count);
>
> During compile, the error we get is this:
>
> file.c:xxx: warning: initialization from incompatible pointer type
Well, a pointer to 'int f(uint *val1, uint *val2, uint count);' _is_ different
from a pointer to 'int f(void)'.
> So how do I specify the initialization so that this error (re: warning)
> goes away? I've tried these:
>
> {"Stuck Address", test_stuck_address(), someval}
> {"Stuck Address", &test_stuck_address, someval}
>
> But neither works. Does it need a special casting? Anyone got a suggestion?
No, you can either change the signature of the function in the structures
declaration:
typedef struct teststruct{
char *name;
int (*fp) (uint *val1, uint *val2, uint count);
int test_type;
} teststruct_0;
or cast the function to the appropriate type during inititalisation . I would
expect the first solution in situations where the functions signature is constant
in the program and the second one in places where the function might have a different
signature depending on other information (callbacks would come to mind).
Hope that helps
Ralf
> --
> Michael J. Hammel |
> The Graphics Muse | Ambivalent? Well, yes and no.
> mjhammel at graphics-muse.org |
> http://www.graphics-muse.com
> _______________________________________________
> Web Page: http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
More information about the LUG
mailing list