A question about struct assignment!

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

    #1

    A question about struct assignment!

    /* test.c */

    struct S
    {
    int a;
    int b;
    };

    /*
    The following 3 statements will cause compile error
    when compiled under MinGW gcc3.4.2,the error message is:
    "error: syntax error before '.' token"
    */
    struct S ss;
    ss.a = 1;
    ss.b = 2;


    int main()
    {
    printf("%d, %d\n", ss.a, ss.b);
    return 0;
    }



    However, when I move the two struct assignment statements
    into main function, as follows:

    int main()
    {
    ss.a = 1;
    ss.b = 2;
    printf("%d, %d\n", ss.a, ss.b);

    return 0;
    }

    it will be successfully compiled!
    I have been greatly puzzled!!!
    Can anyone explain the reason to me?
    Thanks!

  • Keith Thompson

    #2
    Re: A question about struct assignment!

    "ehui928" <ehui928@gmail. comwrites:
    /* test.c */
    >
    struct S
    {
    int a;
    int b;
    };
    >
    /*
    The following 3 statements will cause compile error
    when compiled under MinGW gcc3.4.2,the error message is:
    "error: syntax error before '.' token"
    */
    struct S ss;
    ss.a = 1;
    ss.b = 2;
    >
    >
    int main()
    {
    printf("%d, %d\n", ss.a, ss.b);
    return 0;
    }
    >
    >
    >
    However, when I move the two struct assignment statements
    into main function, as follows:
    >
    int main()
    {
    ss.a = 1;
    ss.b = 2;
    printf("%d, %d\n", ss.a, ss.b);
    >
    return 0;
    }
    >
    it will be successfully compiled!
    Statements are legal only inside function definitions.

    --
    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.

    Comment

    • Richard Tobin

      #3
      Re: A question about struct assignment!

      In article <1158554307.312 316.25890@m7g20 00cwm.googlegro ups.com>,
      ehui928 <ehui928@gmail. comwrote:
      >/*
      >The following 3 statements will cause compile error
      >when compiled under MinGW gcc3.4.2,the error message is:
      "error: syntax error before '.' token"
      >*/
      >struct S ss;
      >ss.a = 1;
      >ss.b = 2;
      Try simplifying:

      int a;
      a = 1;

      Does the problem go away?

      -- Richard

      Comment

      Working...