using struct pointer before declaration

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Serve Laurijssen

    using struct pointer before declaration

    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.


  • Eric

    #2
    Re: using struct pointer before declaration

    Serve Laurijssen <ik@hier.nl> wrote:
    [color=blue]
    > typedef struct Y
    > {
    > void (*f)(X *x);
    > } Y;
    >
    > typedef struct X
    > {
    > int x;
    > } X;[/color]

    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... ===

    Comment

    • Eric Sosman

      #3
      Re: using struct pointer before declaration

      Serve Laurijssen wrote:[color=blue]
      >
      > 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.[/color]

      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@sun .com

      Comment

      • Pieter Droogendijk

        #4
        Re: using struct pointer before declaration

        On Fri, 25 Jul 2003 20:25:49 +0200
        "Serve Laurijssen" <ik@hier.nl> wrote:
        [color=blue]
        > How can I get the following to compile?
        >
        > typedef struct Y
        > {
        > void (*f)(X *x);
        > } Y;
        >
        > typedef struct X
        > {
        > int x;
        > } X;[/color]

        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,"a dceoX$_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);r eturn!s?10:10+* k;}

        Comment

        • Nick Austin

          #5
          Re: using struct pointer before declaration

          On Fri, 25 Jul 2003 20:25:49 +0200, "Serve Laurijssen" <ik@hier.nl>
          wrote:
          [color=blue]
          >How can I get the following to compile?
          >
          >typedef struct Y
          >{
          > void (*f)(X *x);
          >} Y;
          >
          >typedef struct X
          >{
          > int x;
          >} X;[/color]

          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.

          Comment

          • Arthur J. O'Dwyer

            #6
            Re: using struct pointer before declaration


            On Fri, 25 Jul 2003, Serve Laurijssen wrote:[color=blue]
            >
            > "Pieter Droogendijk" <gin@binky.home unix.org> wrote in message[color=green]
            > >
            > > typedef struct Y Y;
            > > typedef struct X X;
            > >
            > > struct Y {
            > > void (*f)(X *x);
            > > };
            > >
            > > struct X {
            > > int x;
            > > };[/color]
            >
            > This certainly looks like it's working. I don't mean to be an ass, but
            > it is correct C right?[/color]

            Yes.

            Comment

            • Jarno A Wuolijoki

              #7
              Re: using struct pointer before declaration

              On Fri, 25 Jul 2003, Eric Sosman wrote:
              [color=blue][color=green]
              > > How can I get the following to compile?
              > > ...[/color]
              >
              > 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.[/color]

              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.

              Comment

              • Emmanuel Delahaye

                #8
                Re: using struct pointer before declaration

                In 'comp.lang.c', "Serve Laurijssen" <ik@hier.nl> wrote:
                [color=blue]
                > 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.[/color]

                Sounds obvious, but wat's wrong with:

                typedef struct
                {
                int x;
                }
                X;

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

                --
                -ed- emdelYOURBRA@no os.fr [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/

                Comment

                Working...