what's diffs in the prog?

G

guoliang

hi:
in c,i use :

int a[5],b[5]={1,2,3,4,5};
struct {
int c[5];
}aaa,bbb;

bbb.c[0]=bbb.c[1]=bbb.c[2]=bbb.c[3]=bbb.c[4]=2;

1) a=b; //compile error;
2) aaa=bbb

1.what's differ between 1),2)?
2.why i can't init bbb by bbb.c[5]={2,2,2,2,2};

thx!!
 
A

Alan Curry

hi:
in c,i use :

int a[5],b[5]={1,2,3,4,5};
struct {
int c[5];
}aaa,bbb;

bbb.c[0]=bbb.c[1]=bbb.c[2]=bbb.c[3]=bbb.c[4]=2;

1) a=b; //compile error;
2) aaa=bbb

1.what's differ between 1),2)?

One involves arrays, the other involves structs. Assigning structs is legal,
assigning arrays isn't.
2.why i can't init bbb by bbb.c[5]={2,2,2,2,2};

Pick one of these:

struct { int c[5]; } aaa, bbb={.c={2,2,2,2,2}};

struct { int c[5]; } aaa, bbb={{2,2,2,2,2}};

struct { int c[5]; } aaa, bbb={2,2,2,2,2};

struct { int c[5]; } aaa, bbb;
memcpy(&bbb.c, (int[5]){2,2,2,2,2});

struct foo { int c[5]; } aaa, bbb;
bbb=(struct foo){.c={2,2,2,2,2}};

struct foo { int c[5]; } aaa, bbb;
bbb=(struct foo){{2,2,2,2,2}};
 
N

Nick Keighley

guoliang said:
in c,i use :

int a[5],b[5]={1,2,3,4,5};
struct {
int c[5];
}aaa,bbb;

bbb.c[0]=bbb.c[1]=bbb.c[2]=bbb.c[3]=bbb.c[4]=2;

1) a=b; //compile error;
2) aaa=bbb

1.what's differ between 1),2)?
2.why i can't init bbb by bbb.c[5]={2,2,2,2,2};

because this is assignment not initialisation, although the syntax
looks superficially similar they are not the same thing.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top