using struct pointer before declaration

Discussion in 'C Programming' started by Serve Laurijssen, Jul 25, 2003.

  1. How can I get the following to compile?

    typedef struct Y
    {
    void (*f)(X *x);
    } Y;

    typedef struct X
    {
    int x;
    } X;

    Just forward declaring X doesn't work and I can't put the word struct before
    X, because the code is generated.
    Serve Laurijssen, Jul 25, 2003
    #1
    1. Advertising

  2. Serve Laurijssen

    Eric Guest

    Serve Laurijssen <> wrote:

    > typedef struct Y
    > {
    > void (*f)(X *x);
    > } Y;
    >
    > typedef struct X
    > {
    > int x;
    > } X;


    Did you try:

    typedef struct X
    {
    int x;
    } X;

    typedef struct Y
    {
    void (*f)(X *x);
    } Y;

    ?

    Famous last words: Works for me.


    --
    == Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
    "Therefore the considerations of the intelligent always include both
    benefit and harm." - Sun Tzu
    == Insults, like violence, are the last refuge of the incompetent... ===
    Eric, Jul 25, 2003
    #2
    1. Advertising

  3. Serve Laurijssen

    Eric Sosman Guest

    Serve Laurijssen wrote:
    >
    > How can I get the following to compile?
    >
    > typedef struct Y
    > {
    > void (*f)(X *x);
    > } Y;
    >
    > typedef struct X
    > {
    > int x;
    > } X;
    >
    > Just forward declaring X doesn't work and I can't put the word struct before
    > X, because the code is generated.


    The only way to get the code to compile as it stands
    is to use a compiler for some non-C language.

    An obvious way to fix it is to move the declaration of
    `X' so it appears before the first use. But since you say
    that another obvious fix isn't allowable, it's not clear
    that this fix is applicable, either.

    Fixing the program that generates the code is another
    possibility.

    --
    Eric Sosman, Jul 25, 2003
    #3
  4. On Fri, 25 Jul 2003 20:25:49 +0200
    "Serve Laurijssen" <> wrote:

    > How can I get the following to compile?
    >
    > typedef struct Y
    > {
    > void (*f)(X *x);
    > } Y;
    >
    > typedef struct X
    > {
    > int x;
    > } X;


    You probably mean a solution like this:

    typedef struct Y Y;
    typedef struct X X;

    struct Y {
    void (*f)(X *x);
    };

    struct X {
    int x;
    };

    --
    main(int c,char*k,char*s){c>0?main(0,"adceoX$_k6][^hn","-7\
    0#05&'40$.6'+).3+1%30"),puts(""):*s?c=!c?-*s:(putchar(45),c
    ),putchar(main(c,k+=*s-c*-1,s+1)):(s=0);return!s?10:10+*k;}
    Pieter Droogendijk, Jul 25, 2003
    #4
  5. Serve Laurijssen

    Nick Austin Guest

    On Fri, 25 Jul 2003 20:25:49 +0200, "Serve Laurijssen" <>
    wrote:

    >How can I get the following to compile?
    >
    >typedef struct Y
    >{
    > void (*f)(X *x);
    >} Y;
    >
    >typedef struct X
    >{
    > int x;
    >} X;


    You could just swap the order of both typedefs. Or:

    struct X;
    typedef struct X X;

    typedef struct Y
    {
    void (*f)(X *x);
    } Y;

    struct X
    {
    int x;
    };

    Nick.
    Nick Austin, Jul 25, 2003
    #5
  6. On Fri, 25 Jul 2003, Serve Laurijssen wrote:
    >
    > "Pieter Droogendijk" <> wrote in message
    > >
    > > typedef struct Y Y;
    > > typedef struct X X;
    > >
    > > struct Y {
    > > void (*f)(X *x);
    > > };
    > >
    > > struct X {
    > > int x;
    > > };

    >
    > This certainly looks like it's working. I don't mean to be an ass, but
    > it is correct C right?


    Yes.
    Arthur J. O'Dwyer, Jul 25, 2003
    #6
  7. On Fri, 25 Jul 2003, Eric Sosman wrote:

    > > How can I get the following to compile?
    > > ...

    >
    > The only way to get the code to compile as it stands
    > is to use a compiler for some non-C language.
    > Fixing the program that generates the code is another
    > possibility.


    Hmmm.. generalizing:

    generatecode foo.almostc
    fixcode foo.almostc foo.c
    cc foo.c

    The two first lines are the fixed generator and the last
    two a non-C compiler.
    Jarno A Wuolijoki, Jul 25, 2003
    #7
  8. In 'comp.lang.c', "Serve Laurijssen" <> wrote:

    > How can I get the following to compile?
    >
    > typedef struct Y
    > {
    > void (*f)(X *x);
    > } Y;
    >
    > typedef struct X
    > {
    > int x;
    > } X;
    >
    > Just forward declaring X doesn't work and I can't put the word struct
    > before X, because the code is generated.


    Sounds obvious, but wat's wrong with:

    typedef struct
    {
    int x;
    }
    X;

    typedef struct
    {
    void (*f)(X *x);
    }
    Y;

    --
    -ed- [remove YOURBRA before answering me]
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    <blank line>
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
    Emmanuel Delahaye, Jul 26, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. beetle
    Replies:
    2
    Views:
    882
    beetle
    Jan 25, 2005
  2. Zero
    Replies:
    16
    Views:
    638
    Barry Schwarz
    Nov 19, 2005
  3. Ehud Shapira
    Replies:
    20
    Views:
    1,100
    Ehud Shapira
    Jun 30, 2007
  4. slocum
    Replies:
    3
    Views:
    498
    slocum
    Apr 11, 2008
  5. aleksa

    Struct pointer vs. struct array pointer

    aleksa, Feb 20, 2013, in forum: C Programming
    Replies:
    16
    Views:
    447
    Shao Miller
    Feb 20, 2013
Loading...

Share This Page