assig array != struct ?

L

langog.info

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!!
 
R

Richard Heathfield

(e-mail address removed) said:
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)?

You can't assign to an array. You /can/ assign to a struct. Why? Well,
because. There doesn't seem to be any better reason than that. It's
just one of them things.
2.why i can't init bbb by bbb.c[5]={2,2,2,2,2};

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

langog.info

(e-mail address removed) said:
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)?

You can't assign to an array. You /can/ assign to a struct. Why? Well,
because. There doesn't seem to be any better reason than that. It's
just one of them things.
2.why i can't init bbb by bbb.c[5]={2,2,2,2,2};

struct {
int c[5];

} aaa, bbb = { { 2, 2, 2, 2, 2 } };

thx rjh!!
but the answer without reason.
if i want to change the value of bbb ,how to do it simply?
can u figure the c's object modle in memory?
 
N

Nick Keighley

Could you not not use abbreviations like "u"? It is clearer if you use
"I"
rather than "i". Things are clearer if you use standard english rather
than
txt-speak.

Also don't post the same thing multiple times.
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)?

You can't assign to an array. You /can/ assign to a struct. Why? Well,
because. There doesn't seem to be any better reason than that. It's
just one of them things.
2.why i can't init bbb by bbb.c[5]={2,2,2,2,2};

struct {
int c[5];

} aaa, bbb = { { 2, 2, 2, 2, 2 } };

don't quote .sigs (anything after "--<space>") unless you are
commenting
on them

but the answer without reason.

yep. The answer is "because" or "ask Dennis" (the designer of the
language).

if i want to change the value of bbb ,how to do it simply?

I suppose you could set up a default value.

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

/* do things to bbb */

/* reset to default */
bbb = def_bbb;

can u figure the c's object modle in memory?

"model"? Why? Why do you need to know this? What are you trying to do?
The ints of C will be in increasing order in memory. Some types may
need padding
between the array elements, but this is unlikely with ints.
 

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,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top