2

Possible Duplicate:
Why does C++ support memberwise assignment of arrays within structs, but not generally?

Why is it that we can assign the object of one structure to other directly example:

struct student s1,s2;
s2=s1 //is perfectly ok.

but we can not assign one array value to the other eg:

int a[10],b[10];
a=b //is not allowed.

though the element of structure may contain any no of arrays.

Please Explain.

Community
  • 1
  • 1
Sadique
  • 22,572
  • 7
  • 65
  • 91
  • Duplicate of [Why does C++ support memberwise assignment of arrays within structs, but not generally?](http://stackoverflow.com/questions/3437110/why-does-c-support-memberwise-assignment-of-arrays-within-structs-but-not-gene) – James McNellis Sep 07 '10 at 19:24
  • A good question and a good duplicate, but yup nevertheless. – Potatoswatter Sep 07 '10 at 19:29

3 Answers3

2

Array assignment is not allowed at language level for purely historical reasons. Check also this question.

Community
  • 1
  • 1
Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
1

Ultimately, I don't think there is a reason that has much in the way of theoretical basis. I'd guess that it comes down to an expectation that arrays are typically large enough that preventing accidental copying was worthwhile.

By contrast, I think the expectation was that structs would typically be small enough that the convenience of supporting assignment outweighed the risk of possibly doing something unexpectedly expensive.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • @Xaero: yes, it *can*. That's why I said "expectation". You *can* have a big struct or a small array, but compiler authors *expected* that arrays would often be large, but structs would usually be smaller. FWIW, the C89 rationale just says: "Structure assignment has been added: its use was foreshadowed even in K&R, and many existing implementations already support it." – Jerry Coffin Sep 07 '10 at 19:32
0

This is how it is specified to be. If you need assignable arrays, use std::vector:

std::vector< int> a(10), b(10);
a = b;

The rule for default assignment operator for structs (and classes) is that it calls the assignment operators for base structs/classes and then the assignment operators for each data member.

Cătălin Pitiș
  • 14,123
  • 2
  • 39
  • 62
  • If assignment operator for each data member is applied, how is it arrays inside structs are copied? I think it's copied byte per byte, right? – Shamim Hafiz - MSFT Sep 07 '10 at 19:01
  • Maybe, maybe not. If you have an array like MyComplicatedType[], then the array will be copied by invoking the MyComplicatedType copy constructor once for each array element. – David Seiler Sep 07 '10 at 19:03
  • I dont need a solution for vector... my question is why it doesnt happen in case of arrays?? – Sadique Sep 07 '10 at 19:10
  • If you need an "assignable array," you should use a `std::array` (or `std::tr1::array` or `boost::array`). A `std::vector` is not always appropriate, especially if you need an array with a small number of elements and the number is known at compile time. – James McNellis Sep 07 '10 at 19:30
  • BTW is bit-wise or byte-wise? Can anyone confirm? – Sadique Sep 07 '10 at 19:33