initializing struct

Discussion in 'C Programming' started by Carramba, Jun 11, 2007.

  1. Carramba

    Carramba Guest

    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?
     
    Carramba, Jun 11, 2007
    #1
    1. Advertising

  2. Carramba

    Ian Collins Guest

    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.
     
    Ian Collins, Jun 11, 2007
    #2
    1. Advertising

  3. "Carramba" <> schrieb im Newsbeitrag
    news:f4j57g$nov$...
    > 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
     
    Joachim Schmitz, Jun 11, 2007
    #3
  4. "Ian Collins" <> schrieb im Newsbeitrag
    news:...
    > 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
     
    Joachim Schmitz, Jun 11, 2007
    #4
  5. Carramba

    Ian Collins Guest

    Joachim Schmitz wrote:
    > "Ian Collins" <> schrieb im Newsbeitrag
    > news:...
    >> 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.
     
    Ian Collins, Jun 11, 2007
    #5
  6. Carramba

    Carramba Guest

    Joachim Schmitz skrev:
    > "Carramba" <> schrieb im Newsbeitrag

    news:f4j57g$nov$...
    >> 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 : "incompatible 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!
     
    Carramba, Jun 11, 2007
    #6
  7. Carramba

    Ian Collins Guest

    Carramba wrote:
    >
    > Joachim Schmitz skrev:
    >> drop these 2 lines, replace by
    >> A *a = b;

    >
    > This doesn't work : "incompatible 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.
     
    Ian Collins, Jun 11, 2007
    #7
  8. Carramba

    Carramba Guest

    Ian Collins skrev:
    > Carramba wrote:
    >> Joachim Schmitz skrev:
    >>> drop these 2 lines, replace by
    >>> A *a = b;

    >> This doesn't work : "incompatible 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?
    >
     
    Carramba, Jun 11, 2007
    #8
  9. Carramba

    Ian Collins Guest

    Carramba wrote:
    > Ian Collins skrev:
    >> Carramba wrote:
    >>> Joachim Schmitz skrev:
    >>>> drop these 2 lines, replace by
    >>>> A *a = b;
    >>> This doesn't work : "incompatible 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.
     
    Ian Collins, Jun 11, 2007
    #9
  10. "Carramba" <> schrieb im Newsbeitrag
    news:f4j951$aa4$...
    > Ian Collins skrev:
    >> Carramba wrote:
    >>> Joachim Schmitz skrev:
    >>>> drop these 2 lines, replace by
    >>>> A *a = b;
    >>> This doesn't work : "incompatible 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.
     
    Joachim Schmitz, Jun 11, 2007
    #10
  11. "Carramba" <> schrieb im Newsbeitrag
    news:f4j80h$4v3$...
    >
    > Joachim Schmitz skrev:
    > > "Carramba" <> schrieb im Newsbeitrag

    > news:f4j57g$nov$...
    > >> 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 : "incompatible 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
     
    Joachim Schmitz, Jun 11, 2007
    #11
  12. Carramba <> writes:

    > 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.
     
    Ben Bacarisse, Jun 11, 2007
    #12
  13. Carramba <> writes:
    > 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!). :cool:}

    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_Keith) <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"
     
    Keith Thompson, Jun 11, 2007
    #13
  14. On Mon, 11 Jun 2007 11:57:20 +0200, "Joachim Schmitz"
    <> wrote:

    >"Ian Collins" <> schrieb im Newsbeitrag
    >news:...
    >> 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
     
    Barry Schwarz, Jun 12, 2007
    #14
    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. RA Scheltema
    Replies:
    3
    Views:
    414
    RA Scheltema
    Jan 6, 2004
  2. mark fine
    Replies:
    1
    Views:
    479
    Michael Jørgensen
    Nov 9, 2004
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,433
    Chris Fogelklou
    Apr 20, 2004
  4. John Lam
    Replies:
    4
    Views:
    163
    Robert Klemme
    Oct 28, 2005
  5. Noob
    Replies:
    59
    Views:
    776
    Tim Rentsch
    Jan 24, 2013
Loading...

Share This Page