re-assignment of a struct

M

Mark

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

Mark

Mark said:
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?
 
B

Barry Schwarz

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];
 
B

Barry Schwarz

Mark said:
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.
 
C

Chad

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';
 
B

Ben Pfaff

Chad said:
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.
 
P

Peter Nilsson

Mark said:
Mark said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top