473,322 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Array assignment conundrum

Hi,

I am trying to get my head around this:

int main()
{
typedef int array1[3];

array1 x,y;
y = x; // error: invalid array assignment

struct array2
{
array1 x;
};

array2 a,b;
b = a; // OK, assigns b.x element-wise from a.x
}

Now for array2, since no assignment op is defined, as I understand it a
default assignment op will be invoked which simply assigns individual
members of struct array2... but the only member of array2 is an array1...
which cannot be assigned. What is happening here?

--
Lionel B
Jul 16 '07 #1
9 6886
On Jul 16, 2:37 pm, Lionel B <m...@privacy.netwrote:
Hi,

I am trying to get my head around this:

int main()
{
typedef int array1[3];

array1 x,y;
y = x; // error: invalid array assignment
because array name is not a modifiable lvalue.
struct array2
{
array1 x;
};

array2 a,b;
b = a; // OK, assigns b.x element-wise from a.x
yes. This is same as saying
b[0] = a[0];
b[1] = a[1];
b[2] = a[2]
>
}

Now for array2, since no assignment op is defined, as I understand it a
default assignment op will be invoked which simply assigns individual
members of struct array2... but the only member of array2 is an array1...
which cannot be assigned. What is happening here?
You are not assigning to an array. you are assigning to array members.
That is perfectly allowed.

-N

Jul 16 '07 #2
On Mon, 16 Jul 2007 02:49:45 -0700, Neelesh Bodas wrote:
On Jul 16, 2:37 pm, Lionel B <m...@privacy.netwrote:
>Hi,

I am trying to get my head around this:

int main()
{
typedef int array1[3];

array1 x,y;
y = x; // error: invalid array assignment

because array name is not a modifiable lvalue.
Ok.
> struct array2
{
array1 x;
};

array2 a,b;
b = a; // OK, assigns b.x element-wise from a.x

yes. This is same as saying
b[0] = a[0];
b[1] = a[1];
b[2] = a[2]
Yes, so it seems. My question was: *why*?

(BTW I assume you meant:

b.x[0] = a.x[0];
b.x[1] = a.x[1];
b.x[2] = a.x[2];

since array2 doesn't have an operator[] )
>}

Now for array2, since no assignment op is defined, as I understand it a
default assignment op will be invoked which simply assigns individual
members of struct array2... but the only member of array2 is an
array1... which cannot be assigned. What is happening here?

You are not assigning to an array. you are assigning to array members.
That is perfectly allowed.
I still don't get it. Eg.

struct astruct
{
int i;
};

astruct a,b;
b = a

will do:

b.i = a.i;

By the same logic, I'd have thought:

array2 a,b;
b = a;

would attempt to do:

b.x = a.x;

but since b.x, a.x are arrays - and therefore non-assignable - this
should not be permissable...

--
Lionel B
Jul 16 '07 #3
On 16 Jul, 11:00, Lionel B <m...@privacy.netwrote:
On Mon, 16 Jul 2007 02:49:45 -0700, Neelesh Bodas wrote:
On Jul 16, 2:37 pm, Lionel B <m...@privacy.netwrote:
Hi,
I am trying to get my head around this:
int main()
{
typedef int array1[3];
array1 x,y;
y = x; // error: invalid array assignment
because array name is not a modifiable lvalue.

Ok.
struct array2
{
array1 x;
};
array2 a,b;
b = a; // OK, assigns b.x element-wise from a.x
yes. This is same as saying
b[0] = a[0];
b[1] = a[1];
b[2] = a[2]

Yes, so it seems. My question was: *why*?

(BTW I assume you meant:

b.x[0] = a.x[0];
b.x[1] = a.x[1];
b.x[2] = a.x[2];

since array2 doesn't have an operator[] )
}
Now for array2, since no assignment op is defined, as I understand it a
default assignment op will be invoked which simply assigns individual
members of struct array2... but the only member of array2 is an
array1... which cannot be assigned. What is happening here?
You are not assigning to an array. you are assigning to array members.
That is perfectly allowed.

I still don't get it. Eg.

struct astruct
{
int i;
};

astruct a,b;
b = a

will do:

b.i = a.i;

By the same logic, I'd have thought:

array2 a,b;
b = a;

would attempt to do:

b.x = a.x;

but since b.x, a.x are arrays - and therefore non-assignable - this
should not be permissable...
according to [12.8.8]

"The implicitly-defined copy constructor for class
X performs a memberwise copy of its subobjects."
....
"- if the subobject is an array, each element is copied,
in the manner appropriate to the element type;"
--
Lionel B
regards

DS

Jul 16 '07 #4
On Mon, 16 Jul 2007 03:34:02 -0700, dasjotre wrote:
On 16 Jul, 11:00, Lionel B <m...@privacy.netwrote:
>On Mon, 16 Jul 2007 02:49:45 -0700, Neelesh Bodas wrote:
On Jul 16, 2:37 pm, Lionel B <m...@privacy.netwrote:
Hi,
>I am trying to get my head around this:
>int main()
{
typedef int array1[3];
> array1 x,y;
y = x; // error: invalid array assignment
because array name is not a modifiable lvalue.

Ok.
> struct array2
{
array1 x;
};
> array2 a,b;
b = a; // OK, assigns b.x element-wise from a.x
yes. This is same as saying
b[0] = a[0];
b[1] = a[1];
b[2] = a[2]

Yes, so it seems. My question was: *why*?

(BTW I assume you meant:

b.x[0] = a.x[0];
b.x[1] = a.x[1];
b.x[2] = a.x[2];

since array2 doesn't have an operator[] )
>}
>Now for array2, since no assignment op is defined, as I understand
it a default assignment op will be invoked which simply assigns
individual members of struct array2... but the only member of array2
is an array1... which cannot be assigned. What is happening here?
You are not assigning to an array. you are assigning to array
members. That is perfectly allowed.

I still don't get it. Eg.

struct astruct
{
int i;
};

astruct a,b;
b = a

will do:

b.i = a.i;

By the same logic, I'd have thought:

array2 a,b;
b = a;

would attempt to do:

b.x = a.x;

but since b.x, a.x are arrays - and therefore non-assignable - this
should not be permissable...

according to [12.8.8]

"The implicitly-defined copy constructor for class X performs a
memberwise copy of its subobjects." ...
"- if the subobject is an array, each element is copied, in the manner
appropriate to the element type;"
Ah, cheers. That's it then.

I wonder why, though, that being the case, that element-wise copy
shouldn't apply also to non-class member arrays...?

--
Lionel B
Jul 16 '07 #5
"Lionel B" <me@privacy.netwrote in message
news:f7**********@south.jnrs.ja.net...
On Mon, 16 Jul 2007 03:34:02 -0700, dasjotre wrote:
>On 16 Jul, 11:00, Lionel B <m...@privacy.netwrote:
>>On Mon, 16 Jul 2007 02:49:45 -0700, Neelesh Bodas wrote:
On Jul 16, 2:37 pm, Lionel B <m...@privacy.netwrote:
Hi,

I am trying to get my head around this:

int main()
{
typedef int array1[3];

array1 x,y;
y = x; // error: invalid array assignment

because array name is not a modifiable lvalue.

Ok.

struct array2
{
array1 x;
};

array2 a,b;
b = a; // OK, assigns b.x element-wise from a.x

yes. This is same as saying
b[0] = a[0];
b[1] = a[1];
b[2] = a[2]

Yes, so it seems. My question was: *why*?

(BTW I assume you meant:

b.x[0] = a.x[0];
b.x[1] = a.x[1];
b.x[2] = a.x[2];

since array2 doesn't have an operator[] )

}

Now for array2, since no assignment op is defined, as I understand
it a default assignment op will be invoked which simply assigns
individual members of struct array2... but the only member of array2
is an array1... which cannot be assigned. What is happening here?

You are not assigning to an array. you are assigning to array
members. That is perfectly allowed.

I still don't get it. Eg.

struct astruct
{
int i;
};

astruct a,b;
b = a

will do:

b.i = a.i;

By the same logic, I'd have thought:

array2 a,b;
b = a;

would attempt to do:

b.x = a.x;

but since b.x, a.x are arrays - and therefore non-assignable - this
should not be permissable...

according to [12.8.8]

"The implicitly-defined copy constructor for class X performs a
memberwise copy of its subobjects." ...
"- if the subobject is an array, each element is copied, in the manner
appropriate to the element type;"

Ah, cheers. That's it then.

I wonder why, though, that being the case, that element-wise copy
shouldn't apply also to non-class member arrays...?
The default copy constructor is sometimes refered to a "bitwise copy". The
data is copied across bit for bit with disreguard as to what they actually
contain. This is not totally true, however. If an object in a
structure/class has it's own default copy constructor, that desturctor will
be called, which is why things like std::string and std::vector get copy
constructed corretly using a default constructor. However, for base types
(pointers, ints, arrays, etc...) they are just copied bit for bit.

That is why your array gets copied corretly.
Jul 16 '07 #6
On Mon, 16 Jul 2007 04:20:22 -0700, Jim Langston wrote:
"Lionel B" <me@privacy.netwrote in message
news:f7**********@south.jnrs.ja.net...
>On Mon, 16 Jul 2007 03:34:02 -0700, dasjotre wrote:
>>On 16 Jul, 11:00, Lionel B <m...@privacy.netwrote:
On Mon, 16 Jul 2007 02:49:45 -0700, Neelesh Bodas wrote:
On Jul 16, 2:37 pm, Lionel B <m...@privacy.netwrote:
[...]
>>>I still don't get it. Eg.

struct astruct
{
int i;
};

astruct a,b;
b = a

will do:

b.i = a.i;

By the same logic, I'd have thought:

array2 a,b;
b = a;

would attempt to do:

b.x = a.x;

but since b.x, a.x are arrays - and therefore non-assignable - this
should not be permissable...
according to [12.8.8]

"The implicitly-defined copy constructor for class X performs a
memberwise copy of its subobjects." ... "- if the subobject is an
array, each element is copied, in the manner appropriate to the
element type;"

Ah, cheers. That's it then.

I wonder why, though, that being the case, that element-wise copy
shouldn't apply also to non-class member arrays...?

The default copy constructor is sometimes refered to a "bitwise copy".
The data is copied across bit for bit with disreguard as to what they
actually contain. This is not totally true, however. If an object in a
structure/class has it's own default copy constructor, that desturctor
will be called, which is why things like std::string and std::vector get
copy constructed corretly using a default constructor. However, for
base types (pointers, ints, arrays, etc...) they are just copied bit for
bit.

That is why your array gets copied corretly.
Cheers, yes, I understand that. What I meant to say was: "is there any
good reason why non-member arrays might not also be bitwise copyable?" -
aside from the fact that the standard doesn't allow for it.

--
Lionel B
Jul 16 '07 #7
"Lionel B" <me@privacy.netwrote in message
news:f7**********@south.jnrs.ja.net...
On Mon, 16 Jul 2007 04:20:22 -0700, Jim Langston wrote:
>"Lionel B" <me@privacy.netwrote in message
news:f7**********@south.jnrs.ja.net...
>>On Mon, 16 Jul 2007 03:34:02 -0700, dasjotre wrote:

On 16 Jul, 11:00, Lionel B <m...@privacy.netwrote:
On Mon, 16 Jul 2007 02:49:45 -0700, Neelesh Bodas wrote:
On Jul 16, 2:37 pm, Lionel B <m...@privacy.netwrote:
[...]
>>>>I still don't get it. Eg.
>
struct astruct
{
int i;
};
>
astruct a,b;
b = a
>
will do:
>
b.i = a.i;
>
By the same logic, I'd have thought:
>
array2 a,b;
b = a;
>
would attempt to do:
>
b.x = a.x;
>
but since b.x, a.x are arrays - and therefore non-assignable - this
should not be permissable...
>
>
according to [12.8.8]

"The implicitly-defined copy constructor for class X performs a
memberwise copy of its subobjects." ... "- if the subobject is an
array, each element is copied, in the manner appropriate to the
element type;"

Ah, cheers. That's it then.

I wonder why, though, that being the case, that element-wise copy
shouldn't apply also to non-class member arrays...?

The default copy constructor is sometimes refered to a "bitwise copy".
The data is copied across bit for bit with disreguard as to what they
actually contain. This is not totally true, however. If an object in a
structure/class has it's own default copy constructor, that desturctor
will be called, which is why things like std::string and std::vector get
copy constructed corretly using a default constructor. However, for
base types (pointers, ints, arrays, etc...) they are just copied bit for
bit.

That is why your array gets copied corretly.

Cheers, yes, I understand that. What I meant to say was: "is there any
good reason why non-member arrays might not also be bitwise copyable?" -
aside from the fact that the standard doesn't allow for it.
What if you wanted to do this?

char Foo[] = "ABCDE";
char* Bar = Foo;

Or:
void FooBar( char* parm1, char* parm2 )
{
parm1 = parm2;
}

int main()
{
char Foo[] = "ABCDE";
char* Bar = Foo;

FooBar( Foo, Bar );
}

How is FooBar supposed to know if they are arrays and if to copy them
element by elemet or not?

And what if you passed them as char parm1[], char parm2[], how is FooBar
supposed to know their size?

A lot of time you may have an array without knowing it's size, so it
wouldn't be possible.
Jul 16 '07 #8
On Mon, 16 Jul 2007 05:31:46 -0700, Jim Langston wrote:
"Lionel B" <me@privacy.netwrote in message
news:f7**********@south.jnrs.ja.net...
>On Mon, 16 Jul 2007 04:20:22 -0700, Jim Langston wrote:
>>"Lionel B" <me@privacy.netwrote in message
news:f7**********@south.jnrs.ja.net...
On Mon, 16 Jul 2007 03:34:02 -0700, dasjotre wrote:

On 16 Jul, 11:00, Lionel B <m...@privacy.netwrote:
>On Mon, 16 Jul 2007 02:49:45 -0700, Neelesh Bodas wrote:
On Jul 16, 2:37 pm, Lionel B <m...@privacy.netwrote:
[...]
>I still don't get it. Eg.
>>
> struct astruct
> {
> int i;
> };
>>
> astruct a,b;
> b = a
>>
>will do:
>>
> b.i = a.i;
>>
>By the same logic, I'd have thought:
>>
> array2 a,b;
> b = a;
>>
>would attempt to do:
>>
> b.x = a.x;
>>
>but since b.x, a.x are arrays - and therefore non-assignable - this
>should not be permissable...
>>
>>
according to [12.8.8]
>
"The implicitly-defined copy constructor for class X performs a
memberwise copy of its subobjects." ... "- if the subobject is an
array, each element is copied, in the manner appropriate to the
element type;"

Ah, cheers. That's it then.

I wonder why, though, that being the case, that element-wise copy
shouldn't apply also to non-class member arrays...?

The default copy constructor is sometimes refered to a "bitwise copy".
The data is copied across bit for bit with disreguard as to what they
actually contain. This is not totally true, however. If an object in
a structure/class has it's own default copy constructor, that
desturctor will be called, which is why things like std::string and
std::vector get copy constructed corretly using a default constructor.
However, for base types (pointers, ints, arrays, etc...) they are
just copied bit for bit.

That is why your array gets copied corretly.

Cheers, yes, I understand that. What I meant to say was: "is there any
good reason why non-member arrays might not also be bitwise copyable?"
- aside from the fact that the standard doesn't allow for it.

What if you wanted to do this?

char Foo[] = "ABCDE";
char* Bar = Foo;

Or:
void FooBar( char* parm1, char* parm2 ) {
parm1 = parm2;
}
[...]

Yup, say no more.

Cheers,

--
Lionel B
Jul 16 '07 #9
On 16 Jul, 13:42, Lionel B <m...@privacy.netwrote:
On Mon, 16 Jul 2007 05:31:46 -0700, Jim Langston wrote:
"Lionel B" <m...@privacy.netwrote in message
news:f7**********@south.jnrs.ja.net...
On Mon, 16 Jul 2007 04:20:22 -0700, Jim Langston wrote:
>"Lionel B" <m...@privacy.netwrote in message
news:f7**********@south.jnrs.ja.net...
On Mon, 16 Jul 2007 03:34:02 -0700, dasjotre wrote:
>>>On 16 Jul, 11:00, Lionel B <m...@privacy.netwrote:
On Mon, 16 Jul 2007 02:49:45 -0700, Neelesh Bodas wrote:
On Jul 16, 2:37 pm, Lionel B <m...@privacy.netwrote:
[...]
I still don't get it. Eg.
>>>> struct astruct
{
int i;
};
>>>> astruct a,b;
b = a
>>>>will do:
>>>> b.i = a.i;
>>>>By the same logic, I'd have thought:
>>>> array2 a,b;
b = a;
>>>>would attempt to do:
>>>> b.x = a.x;
>>>>but since b.x, a.x are arrays - and therefore non-assignable - this
should not be permissable...
>>>according to [12.8.8]
>>>"The implicitly-defined copy constructor for class X performs a
memberwise copy of its subobjects." ... "- if the subobject is an
array, each element is copied, in the manner appropriate to the
element type;"
>>Ah, cheers. That's it then.
>>I wonder why, though, that being the case, that element-wise copy
shouldn't apply also to non-class member arrays...?
>The default copy constructor is sometimes refered to a "bitwise copy".
The data is copied across bit for bit with disreguard as to what they
actually contain. This is not totally true, however. If an object in
a structure/class has it's own default copy constructor, that
desturctor will be called, which is why things like std::string and
std::vector get copy constructed corretly using a default constructor.
However, for base types (pointers, ints, arrays, etc...) they are
just copied bit for bit.
>That is why your array gets copied corretly.
Cheers, yes, I understand that. What I meant to say was: "is there any
good reason why non-member arrays might not also be bitwise copyable?"
- aside from the fact that the standard doesn't allow for it.
What if you wanted to do this?
char Foo[] = "ABCDE";
char* Bar = Foo;
Or:
void FooBar( char* parm1, char* parm2 ) {
parm1 = parm2;
}

[...]

Yup, say no more.
in C array to pointer degradation is compulsory
for all expressions of array type except in
few cases such as sizeof or char str[] = "str"
the pointer you get from the conversion can
only be rvalue.

AFAIK C++ copied that rule from C with some
C++ specific exceptions (references,
implicit copy).
Cheers,

--
Lionel B
regards

DS
Jul 16 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: James Mcguire | last post by:
Hi, I frequently do non-initialisation type structure assignment via casting: e.g. struct s{int i,j,k;} mys; .... mys=(struct s){3,4,5};
36
by: Eric Laberge | last post by:
Hi! I'm working on automatically generated code, and need to assign arrays. memcpy is an obvious solution, but it becomes complicated to use in the context I'm working on, ie.: I could use it...
4
by: Ben Voigt | last post by:
I am accustomed to using a pointer to traverse an array in native C++. Here I am trying to copy a slice of a native 2-D array into a managed 2-D array to make it accessible by C#. But piData...
11
by: Mr. Ken | last post by:
For example, int arr; arr = {1,2,3,4,5};
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.