re-assignment of a struct

Discussion in 'C Programming' started by Mark, Dec 9, 2009.

  1. Mark

    Mark Guest

    Hello

    consider the code:

    struct {
    int a;
    int b;
    } p1[] = {
    {0,1}, {0,2}, {0,3},
    {-1,-1}
    };
    ....

    Is there a way to re-assign the structure later, say something like:

    if (some_condition) {
    p1[] = { {1,1}, {2,2}, {3,3}, {4,4}};
    }

    I tried to typedef a new type, but with no luck and with compiler's error:

    typedef struct p{
    int a;
    int b;
    } p;

    p p1[] = {
    {0, 1}, {0, 2}, {0, 3},
    {-1, -1}
    };
    ....
    p p2[] = {{1,2}, {3,4}, {5,6}, {7,8}};
    p1 = p2; /* Error: incompatible types in assignment */

    What am I missing ?
    Thanks in advance.

    --
    Mark
     
    Mark, Dec 9, 2009
    #1
    1. Advertising

  2. Mark

    Mark Guest

    "Mark" <> wrote in message
    news:hfnnv4$779$...
    > typedef struct p{
    > int a;
    > int b;
    > } p;
    >
    > p p1[] = {
    > {0, 1}, {0, 2}, {0, 3},
    > {-1, -1}
    > };
    > ...
    > p p2[] = {{1,2}, {3,4}, {5,6}, {7,8}};
    > p1 = p2; /* Error: incompatible types in assignment */
    >


    Forgot to add that it works with memcpy(). Is there a way to accomplish this
    without using extra variable?

    --
    Mark
     
    Mark, Dec 9, 2009
    #2
    1. Advertising

  3. On Wed, 9 Dec 2009 17:43:57 +0900, "Mark"
    <> wrote:

    >Hello
    >
    >consider the code:
    >
    >struct {
    > int a;
    > int b;
    >} p1[] = {
    > {0,1}, {0,2}, {0,3},
    > {-1,-1}
    >};
    >...
    >
    >Is there a way to re-assign the structure later, say something like:
    >
    >if (some_condition) {
    > p1[] = { {1,1}, {2,2}, {3,3}, {4,4}};
    >}
    >
    >I tried to typedef a new type, but with no luck and with compiler's error:
    >
    >typedef struct p{
    > int a;
    > int b;
    >} p;
    >
    >p p1[] = {
    > {0, 1}, {0, 2}, {0, 3},
    > {-1, -1}
    >};
    >...
    >p p2[] = {{1,2}, {3,4}, {5,6}, {7,8}};
    >p1 = p2; /* Error: incompatible types in assignment */
    >
    >What am I missing ?


    The fact that your assignment is an array assignment, not a struct
    assignment. You would have had no problem with
    p1[0] = p2[0];

    --
    Remove del for email
     
    Barry Schwarz, Dec 9, 2009
    #3
  4. On Wed, 9 Dec 2009 17:56:00 +0900, "Mark"
    <> wrote:

    >"Mark" <> wrote in message
    >news:hfnnv4$779$...
    >> typedef struct p{
    >> int a;
    >> int b;
    >> } p;
    >>
    >> p p1[] = {
    >> {0, 1}, {0, 2}, {0, 3},
    >> {-1, -1}
    >> };
    >> ...
    >> p p2[] = {{1,2}, {3,4}, {5,6}, {7,8}};
    >> p1 = p2; /* Error: incompatible types in assignment */
    >>

    >
    >Forgot to add that it works with memcpy(). Is there a way to accomplish this
    >without using extra variable?


    If you are using a C99 compiler, look up "compound literals". This
    feature will allow you to assign values to all the members of a struct
    in one assignment without the use of an extra variable. (But you
    still cannot specify an array as the left operand of the assignment
    operator.)

    If you are willing to change p1 from an array of struct to a pointer
    to struct, then 6.5.2.5-9 and -10 show how to initialize and assign
    the pointer.

    Depending on the intended use of the pointer p1, 6.5.2.5-11 shows how
    it may even be possible to avoid defining that variable.

    Note that using compound literals may not reduce the size of your
    program since the literals occupy space as described in 6.5.2.5-13.
    But they will make your code a lot harder to read. They are also
    likely to have little impact on performance since the data must be
    copied "under the covers" as opposed to explicitly with memcpy.

    --
    Remove del for email
     
    Barry Schwarz, Dec 9, 2009
    #4
  5. Mark

    Chad Guest

    On Dec 9, 1:03 am, Richard Heathfield <> wrote:
    > In <hfnnv4$>, Mark wrote:
    > > Hello

    >
    > > consider the code:

    >
    > > struct {
    > >     int a;
    > >     int b;
    > > } p1[] = {
    > >     {0,1}, {0,2}, {0,3},
    > >     {-1,-1}
    > > };
    > > ...

    >
    > > Is there a way to re-assign the structure later, say something like:

    >
    > > if (some_condition) {
    > >     p1[] = { {1,1}, {2,2}, {3,3}, {4,4}};
    > > }

    >
    > Here, you aren't trying to reassign a /struct/, but an /array/ of
    > struct. C doesn't allow that. In C, arrays are not modifiable
    > lvalues, so trying to assign to them violates the constraint in C89's
    > 3.3.16 and C99's 6.5.16(2).
    >
    > You can, of course, cheat by wrapping the array of struct in another
    > struct, like this:
    >
    > struct inner { int a; int b; };
    > struct outer { struct inner data[4] };
    > struct outer p1 = { { { 0, 1 }, { 0, 2 }, { 0, 3 }, { -1, -1 } } };
    > struct outer p2 = { { { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 } } };
    >
    > workwithp1forabit(&p1);
    > p1 = p2;
    > workwithp1somemore(&p1);
    >


    If C, arrays are not modifiable lvalues, then how come I can do
    something like the following...

    char arr[]="chad";
    arr[1]='H';
     
    Chad, Dec 9, 2009
    #5
  6. Mark

    Ben Pfaff Guest

    Chad <> writes:

    > If C, arrays are not modifiable lvalues, then how come I can do
    > something like the following...
    >
    > char arr[]="chad";
    > arr[1]='H';


    You didn't modify the array (arr). You modified one of its
    elements (arr[1]). That's fine, since array elements are not
    (necessarily) arrays themselves.
    --
    Ben Pfaff
    http://benpfaff.org
     
    Ben Pfaff, Dec 9, 2009
    #6
  7. "Mark" <> wrote:
    > "Mark" <> wrote in message
    > > typedef struct p{
    > >    int a;
    > >    int b;
    > > } p;
    > >
    > > p p1[] = {
    > >    {0, 1}, {0, 2}, {0, 3},
    > >    {-1, -1}
    > > };
    > > ...
    > > p p2[] = {{1,2}, {3,4}, {5,6}, {7,8}};
    > > p1 = p2;  /* Error: incompatible types in assignment */

    >
    > Forgot to add that it works with memcpy(). Is there a way
    > to accomplish this without using extra variable?


    Why do you care? Write it the way that is clear and let
    the compiler do what it does. Even if you don't declare
    an explicit temporary variable, chances are the compiler
    will create a hidden one anyway.

    You seem to be searching for a solution to a problem that
    doesn't exist.

    --
    Peter
     
    Peter Nilsson, Dec 9, 2009
    #7
    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. Chris Fogelklou
    Replies:
    36
    Views:
    1,405
    Chris Fogelklou
    Apr 20, 2004
  2. nagy
    Replies:
    36
    Views:
    1,023
    Terry Reedy
    Jul 20, 2006
  3. Eric Laberge

    Array assignment via struct

    Eric Laberge, Aug 4, 2005, in forum: C Programming
    Replies:
    36
    Views:
    928
    Dave Thompson
    Aug 14, 2005
  4. Chris
    Replies:
    34
    Views:
    1,551
  5. Noob
    Replies:
    25
    Views:
    1,495
    Nick Keighley
    Dec 9, 2009
Loading...

Share This Page