Re: Pointers to functions in structures trubble!



addinall@xxxxxxxxxxxx wrote:
Ian Collins wrote:

addinall@xxxxxxxxxxxx wrote:

Ian Collins wrote:


What's the prototype for __life? Generally not a good idea to use '__'
at the beginning of a function name.


Yeah. I thought an earlier prblem may have been name duplication
so.....

I got it to compile by...


<snipped code>

new_heartbeat->life = &__life ;

The & isn't requited and you realy should remove the double underscore
from your names. All identifiers starting with double underscore are
reserved.

So... it compiles and links without errors just on a simple

cc t.c -o t

Try to run it

../t
Segmentation fault.

Something is stuffed and I think it may be me! ;-)

Star with a simple test case, like

#include <stdio.h>

void f() { puts("hello\n"); }

struct X
{
void (*fn)();
};

int
main()
{
struct X x;

x.fn = f;

x.fn();
}

--
Ian Collins.
.



Relevant Pages

  • Re: Segmentation fault!
    ... void *content; ... which always start with underscore). ... just write out "struct" as needed.) ... This is a C99 feature: a structure that ends with a member of type ...
    (comp.lang.c)
  • Re: Library bug or my fault?
    ... void memcpy ... struct Foo { ... 29 void cp(const Foo *foo) ... You have no definition for cp2so the code above won't compile. ...
    (comp.lang.c)
  • Re: A non-const reference may only be bound to an lvalue?
    ... To my surprise, I have tested that the following code can compile, ... struct X { ... void f; ... There are legitimate reasons to want to do that, but I've come to accept it ...
    (microsoft.public.vc.language)
  • Re: beginners question
    ... > "I'm trying to compile this code ... > int i; ... > void main ... > I tried it with 'struct A' and it worked fine. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Problems with void Template Type Parameters and Zero-Length Arrays
    ... > The problem is that, when len is a multiple of sizeof, the pad ... I need to find some way to make this compile in that case. ... struct base_buffer_t ... This next line can't compile if the substitution T = void is done. ...
    (comp.lang.cpp)