initializing struct

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

    initializing struct

    Hi!
    I have a big struct and I want to initialize it at once, but I get parse
    error before "{" compiler error. can't it by done?

    #include <stdio.h>
    #include <stdlib.h>

    typedef struct{
    int b;
    int c;
    int d;
    int e;
    }A;

    A *a;

    int main(void) {

    a = malloc(4 * sizeof (*a));
    a = {1,1,4,5}; //there is error from my compiler
    printf("%d ; %d\n", a->b+a->d, a->c*a->e);

    free(a);
    return 0;
    }

    is this is bad way to init struct how one should do?
  • Ian Collins

    #2
    Re: initializing struct

    Carramba wrote:
    Hi!
    I have a big struct and I want to initialize it at once, but I get parse
    error before "{" compiler error. can't it by done?
    >
    #include <stdio.h>
    #include <stdlib.h>
    >
    typedef struct{
    int b;
    int c;
    int d;
    int e;
    }A;
    >
    A *a;
    >
    int main(void) {
    >
    a = malloc(4 * sizeof (*a));
    a = {1,1,4,5}; //there is error from my compiler
    You can't assign a static initialiser to a pointer. You could write

    A a[4] = {{1},{1},{4},{5 }};

    --
    Ian Collins.

    Comment

    • Joachim Schmitz

      #3
      Re: initializing struct

      "Carramba" <carramba@examp le.comschrieb im Newsbeitrag
      news:f4j57g$nov $1@aioe.org...
      Hi!
      I have a big struct and I want to initialize it at once, but I get parse
      No, you have a pointer to a struct.
      error before "{" compiler error. can't it by done?
      >
      #include <stdio.h>
      #include <stdlib.h>
      >
      typedef struct{
      int b;
      int c;
      int d;
      int e;
      }A;
      That's not particulary large...
      >
      A *a;
      A b = {1,2,3,4};
      int main(void) {
      >
      a = malloc(4 * sizeof (*a));
      a = {1,1,4,5}; //there is error from my compiler
      drop these 2 lines, replace by
      A *a = b;
      printf("%d ; %d\n", a->b+a->d, a->c*a->e);
      >
      free(a);
      return 0;
      }
      >
      is this is bad way to init struct how one should do?
      Can be done only as part of the definition, like I did above. And not to a
      pointer to struct.

      Bye, Jojo


      Comment

      • Joachim Schmitz

        #4
        Re: initializing struct

        "Ian Collins" <ian-news@hotmail.co mschrieb im Newsbeitrag
        news:5d4k30F31c 3dqU11@mid.indi vidual.net...
        Carramba wrote:
        >Hi!
        >I have a big struct and I want to initialize it at once, but I get parse
        >error before "{" compiler error. can't it by done?
        >>
        >#include <stdio.h>
        >#include <stdlib.h>
        >>
        >typedef struct{
        > int b;
        > int c;
        > int d;
        > int e;
        >}A;
        >>
        >A *a;
        >>
        >int main(void) {
        >>
        > a = malloc(4 * sizeof (*a));
        > a = {1,1,4,5}; //there is error from my compiler
        >
        You can't assign a static initialiser to a pointer. You could write
        >
        A a[4] = {{1},{1},{4},{5 }};
        Which would set a[0].b to 1, a[1].b to 1, a[2].b to 4 and a[3].b to 5,
        leaving all the c, d and e members uninitialized resp. set to 0.

        A a = {1, 1, 4, 5};
        Would do...


        Bye, Jojo


        Comment

        • Ian Collins

          #5
          Re: initializing struct

          Joachim Schmitz wrote:
          "Ian Collins" <ian-news@hotmail.co mschrieb im Newsbeitrag
          news:5d4k30F31c 3dqU11@mid.indi vidual.net...
          >Carramba wrote:
          >>Hi!
          >>I have a big struct and I want to initialize it at once, but I get parse
          >>error before "{" compiler error. can't it by done?
          >>>
          >>#include <stdio.h>
          >>#include <stdlib.h>
          >>>
          >>typedef struct{
          >> int b;
          >> int c;
          >> int d;
          >> int e;
          >>}A;
          >>>
          >>A *a;
          >>>
          >>int main(void) {
          >>>
          >> a = malloc(4 * sizeof (*a));
          >> a = {1,1,4,5}; //there is error from my compiler
          >You can't assign a static initialiser to a pointer. You could write
          >>
          >A a[4] = {{1},{1},{4},{5 }};
          Which would set a[0].b to 1, a[1].b to 1, a[2].b to 4 and a[3].b to 5,
          leaving all the c, d and e members uninitialized resp. set to 0.
          >
          Correct, that's what I thought the OP wanted.

          --
          Ian Collins.

          Comment

          • Carramba

            #6
            Re: initializing struct


            Joachim Schmitz skrev:
            "Carramba" <carramba@examp le.comschrieb im Newsbeitrag
            news:f4j57g$nov $1@aioe.org...
            >Hi!
            >I have a big struct and I want to initialize it at once, but I get parse
            No, you have a pointer to a struct.
            >
            >error before "{" compiler error. can't it by done?
            >>
            >#include <stdio.h>
            >#include <stdlib.h>
            >>
            >typedef struct{
            >int b;
            >int c;
            >int d;
            >int e;
            >}A;
            That's not particulary large...
            I only wrote compilable small program to show my problem...
            I don't think there would by a point to post huge struct then this one
            serves purpose.

            >A *a;
            A b = {1,2,3,4};
            >
            >int main(void) {
            >>
            >a = malloc(4 * sizeof (*a));
            >a = {1,1,4,5}; //there is error from my compiler
            drop these 2 lines, replace by
            A *a = b;
            This doesn't work : "incompatib le types in assignment"
            >printf("%d ; %d\n", a->b+a->d, a->c*a->e);
            >>
            > free(a);
            > return 0;
            >}
            >>
            >is this is bad way to init struct how one should do?
            Can be done only as part of the definition, like I did above. And not
            to a pointer to struct.
            >
            Bye, Jojo
            Thank you!

            Comment

            • Ian Collins

              #7
              Re: initializing struct

              Carramba wrote:
              >
              Joachim Schmitz skrev:
              >drop these 2 lines, replace by
              >A *a = b;
              >
              This doesn't work : "incompatib le types in assignment"
              >
              Not surprising, he probably intended to write A *a = *b;

              What exactly to do want to do, create a number of structures and
              initialise them all to the same value, or different values?

              --
              Ian Collins.

              Comment

              • Carramba

                #8
                Re: initializing struct

                Ian Collins skrev:
                Carramba wrote:
                >Joachim Schmitz skrev:
                >>drop these 2 lines, replace by
                >>A *a = b;
                >This doesn't work : "incompatib le types in assignment"
                >>
                Not surprising, he probably intended to write A *a = *b;
                you mean A *a= &b; ?
                What exactly to do want to do, create a number of structures and
                initialise them all to the same value, or different values?
                >

                Comment

                • Ian Collins

                  #9
                  Re: initializing struct

                  Carramba wrote:
                  Ian Collins skrev:
                  >Carramba wrote:
                  >>Joachim Schmitz skrev:
                  >>>drop these 2 lines, replace by
                  >>>A *a = b;
                  >>This doesn't work : "incompatib le types in assignment"
                  >>>
                  >Not surprising, he probably intended to write A *a = *b;
                  you mean A *a= &b; ?
                  >What exactly to do want to do, create a number of structures and
                  >initialise them all to the same value, or different values?
                  >>
                  oops...

                  --
                  Ian Collins.

                  Comment

                  • Joachim Schmitz

                    #10
                    Re: initializing struct

                    "Carramba" <carramba@examp le.comschrieb im Newsbeitrag
                    news:f4j951$aa4 $1@aioe.org...
                    Ian Collins skrev:
                    >Carramba wrote:
                    >>Joachim Schmitz skrev:
                    >>>drop these 2 lines, replace by
                    >>>A *a = b;
                    >>This doesn't work : "incompatib le types in assignment"
                    >>>
                    >Not surprising, he probably intended to write A *a = *b;
                    you mean A *a= &b; ?
                    Yes, sorry, that's what I meant.


                    Comment

                    • Joachim Schmitz

                      #11
                      Re: initializing struct

                      "Carramba" <carramba@examp le.comschrieb im Newsbeitrag
                      news:f4j80h$4v3 $1@aioe.org...
                      >
                      Joachim Schmitz skrev:
                      "Carramba" <carramba@examp le.comschrieb im Newsbeitrag
                      news:f4j57g$nov $1@aioe.org...
                      Hi!
                      I have a big struct and I want to initialize it at once, but I get
                      parse
                      No, you have a pointer to a struct.
                      error before "{" compiler error. can't it by done?
                      >
                      #include <stdio.h>
                      #include <stdlib.h>
                      >
                      typedef struct{
                      int b;
                      int c;
                      int d;
                      int e;
                      }A;
                      That's not particulary large...
                      >
                      I only wrote compilable small program to show my problem...
                      I don't think there would by a point to post huge struct then this one
                      serves purpose.
                      OK, I sort of guessed that... but couldn't resist that remark.

                      A *a;
                      A b = {1,2,3,4};
                      int main(void) {
                      >
                      a = malloc(4 * sizeof (*a));
                      a = {1,1,4,5}; //there is error from my compiler
                      drop these 2 lines, replace by
                      A *a = b;
                      >
                      This doesn't work : "incompatib le types in assignment"
                      A *a = &b;
                      printf("%d ; %d\n", a->b+a->d, a->c*a->e);
                      >
                      free(a);
                      return 0;
                      }
                      >
                      is this is bad way to init struct how one should do?
                      Can be done only as part of the definition, like I did above. And not
                      to a pointer to struct.
                      Bye, Jojo


                      Comment

                      • Ben Bacarisse

                        #12
                        Re: initializing struct

                        Carramba <carramba@examp le.comwrites:
                        Hi!
                        I have a big struct and I want to initialize it at once, but I get
                        parse error before "{" compiler error. can't it by done?
                        >
                        #include <stdio.h>
                        #include <stdlib.h>
                        >
                        typedef struct{
                        int b;
                        int c;
                        int d;
                        int e;
                        }A;
                        >
                        A *a;
                        >
                        int main(void) {
                        >
                        a = malloc(4 * sizeof (*a));
                        a = {1,1,4,5}; //there is error from my compiler
                        printf("%d ; %d\n", a->b+a->d, a->c*a->e);
                        >
                        free(a);
                        return 0;
                        }
                        >
                        is this is bad way to init struct how one should do?
                        You do know you are allocating space for an array of 4 structs? If that
                        is indeed what you want you can do it like this:

                        struct A local_copy[] = {{1}, {2}, {3}, {4}};
                        a = malloc(4 * sizeof *a);
                        if (a)
                        memcpy(a, local_copy, sizeof local_copy);

                        In C99 you can use a compound literal:

                        a = malloc(4 * sizeof *a);
                        if (a)
                        memcpy(a, (struct A[]){{1}, {2}, {3}, {4}}, 4 * sizeof *a);

                        This has portability issues (limited C99 support) and is messier to
                        write. Note also that I have imitated other posters and explicitly
                        set only the "b" structure member in each array element.

                        --
                        Ben.

                        Comment

                        • Keith Thompson

                          #13
                          Re: initializing struct

                          Carramba <carramba@examp le.comwrites:
                          I have a big struct and I want to initialize it at once, but I get
                          parse error before "{" compiler error. can't it by done?
                          >
                          #include <stdio.h>
                          #include <stdlib.h>
                          >
                          typedef struct{
                          int b;
                          int c;
                          int d;
                          int e;
                          }A;
                          >
                          A *a;
                          >
                          int main(void) {
                          >
                          a = malloc(4 * sizeof (*a));
                          a = {1,1,4,5}; //there is error from my compiler
                          printf("%d ; %d\n", a->b+a->d, a->c*a->e);
                          >
                          free(a);
                          return 0;
                          }
                          >
                          is this is bad way to init struct how one should do?
                          Well, since it doesn't compile, yeah, it's a bad way (but it's
                          quick!). 8-)}

                          You allocate space for 4 structures, but you ignore all but the first.
                          I assume your real program doesn't do that. Also, you should always
                          check the result of malloc, even if you just abort the program if it
                          fails.

                          The problem with your attempted assignment is that {1,1,4,5} isn't an
                          expression; it can be used in an initializer, but not on the right
                          hand side of an assignment. And it's an initializer for a structure,
                          not for a pointer.

                          So declare a const object that you can use in assignment:

                          const A A_init = { 1, 1, 4, 5 };

                          and assign its value to a[0], or *a, or whatever.

                          --
                          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."
                          -- Antony Jay and Jonathan Lynn, "Yes Minister"

                          Comment

                          • CBFalconer

                            #14
                            Re: initializing struct

                            Carramba wrote:
                            >
                            I have a big struct and I want to initialize it at once, but I
                            get parse error before "{" compiler error. can't it by done?
                            >
                            #include <stdio.h>
                            #include <stdlib.h>
                            >
                            typedef struct{
                            int b;
                            int c;
                            int d;
                            int e;
                            } A;
                            >
                            A *a;
                            >
                            int main(void) {
                            a = malloc(4 * sizeof (*a));
                            a = {1,1,4,5}; //there is error from my compiler
                            printf("%d ; %d\n", a->b+a->d, a->c*a->e);
                            free(a);
                            return 0;
                            }
                            >
                            is this is bad way to init struct how one should do?
                            You can only use such initialization during a declaration, and
                            never for malloced space. Use:

                            a->b = a->c = 1; a->d = 4; a->e = 5;

                            Alternatively, you can assign from an existing structure:

                            A b = {1, 1, 4, 5}, *a;
                            ....
                            if (!(a = malloc(sizeof *a))) /* memory not available */;
                            else {
                            *a = b;
                            ...

                            ALWAYS check the results from malloc.

                            --
                            <http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
                            <http://www.securityfoc us.com/columnists/423>
                            <http://www.aaxnet.com/editor/edit043.html>
                            <http://kadaitcha.cx/vista/dogsbreakfast/index.html>
                            cbfalconer at maineline dot net



                            --
                            Posted via a free Usenet account from http://www.teranews.com

                            Comment

                            • Barry Schwarz

                              #15
                              Re: initializing struct

                              On Mon, 11 Jun 2007 11:57:20 +0200, "Joachim Schmitz"
                              <nospam.jojo@sc hmitz-digital.dewrote :
                              >"Ian Collins" <ian-news@hotmail.co mschrieb im Newsbeitrag
                              >news:5d4k30F31 c3dqU11@mid.ind ividual.net...
                              >Carramba wrote:
                              >>Hi!
                              >>I have a big struct and I want to initialize it at once, but I get parse
                              >>error before "{" compiler error. can't it by done?
                              >>>
                              >>#include <stdio.h>
                              >>#include <stdlib.h>
                              >>>
                              >>typedef struct{
                              >> int b;
                              >> int c;
                              >> int d;
                              >> int e;
                              >>}A;
                              >>>
                              >>A *a;
                              >>>
                              >>int main(void) {
                              >>>
                              >> a = malloc(4 * sizeof (*a));
                              >> a = {1,1,4,5}; //there is error from my compiler
                              >>
                              >You can't assign a static initialiser to a pointer. You could write
                              >>
                              >A a[4] = {{1},{1},{4},{5 }};
                              >Which would set a[0].b to 1, a[1].b to 1, a[2].b to 4 and a[3].b to 5,
                              >leaving all the c, d and e members uninitialized resp. set to 0.
                              An uninitialized variable has an indeterminate value.

                              In cases where an aggregate is partially explicitly initialized (such
                              as this), the remaining variables that are not explicitly initialized
                              are implicitly initialized to the appropriate form of 0.


                              Remove del for email

                              Comment

                              Working...