Invalid array assignment

May 7, 2011 at 2:02am
C++ noobie here, just looking for some help. I have a class and a function(grossly simplified from what I have, but has all the basics):


1
2
3
4
5
6
7
8
9
10
11
12
class names{
public:
char firstname[128];
char lastname[128];
};

void somefunctionname(){
char firstname[128];
char lastname[128];
names* thesky=new names;
thesky->firstname=firstname;
thesky->lastname=lastname;


Then it churns out an "invalid array assignment" at the two "thesky->" lines.

So why can't I take an array in a function and shove them into an array in a class, or did I do something completely wrong?

EDIT: I managed to avoid it by using a loop to copy each letter, but I'm still curious as to why the arrays don't replace by themselves.
Last edited on May 7, 2011 at 2:19am
May 7, 2011 at 3:18am
You can't just use the assignment operator on the whole array. Actually, an array is a pointer to a block of memory. So, saying array1 = array2; is like trying to change the address of the array, not the values it stores, which you can't do.
May 7, 2011 at 3:31am
That's right. You'll have to do a element-wise copy, either with a for loop or std::copy().
Topic archived. No new replies allowed.