Why can't I assign an array variable directly

2019-01-20 00:34发布

Why does the following assignment not work? I would like a low-level explanation if possible. Also, here's the compiler error I get: incompatible types in assignment of 'char*' to 'char [20]'

class UCSDStudent {

  char name[20];

  public:

    UCSDStudent( char name[] ) {
      //this-> name = name; does not work! Please explain why not
      strcopy( this -> copy, copy ); //works 
    }

};

5条回答
放我归山
2楼-- · 2019-01-20 01:13

c-style arrays are not copyable like that, if you want to copy one, you have to copy the content instead:

int arr1[10] = {1,2,3,4,5,6,7,8,9,10};
int arr2[10];
std::copy(arr1, arr1+10, arr2);
// std::copy() lives in <algorithm>
// content of arr2 is a now copy of arr1

However, low-level features like c-style arrays, are best avoided.

If what you really wanted was a variable length string, use std::string instead, it supports copying via assignment operator.

If you really wanted a fixed length array, use std::array<T,N> instead, it also supports copying via assignment operator.

Also note, a recommendation is that parameter names and member variable names are distinct, except for constructors where you use the member initialization syntax:

#include <string>

class UCSDStudent
{
    std::string name;
public:
    UCSDStudent( std::string name )
        : name(name)
    {
    }
    void SetName( std::string new_name )
    {
        name = new_name;
    }
};

Also note, if you planning on having setters and getters for all member variables, may I recommend public data instead, at least if the class does not have an class-invariant.

查看更多
虎瘦雄心在
3楼-- · 2019-01-20 01:16

Let's take this example.

int arr1[10] = {1,2,3,4,5,6,7,8,9,10};

In case of C++, the array name arr1 does not get a separate memory location. It's true that arr1 sort of holds the memory location of arr1[0] but that is more of an oversimplification. The way arrays are referenced in C++ is via the symbol table. When an array is created, what actually happens is corresponding to arr1 an entry is created in the symbol table at compile time and this entry contains the information about the data-type arr1 refers to (which here is an integer-type array) and the address of arr1[0]. Now, here lies the interesting thing. Once an entry is created in the symbol table, C++ does not allow for it be changed.

If you do arr1=arr2, what you are essentially trying to do is reassign arr1 to the entry corresponding to arr2 in the symbol table. But arr1 has already been assigned some value in the symbol table. So C++ will give you an error.

查看更多
我只想做你的唯一
4楼-- · 2019-01-20 01:17

Because when you have a function call like this UCSDStudent( char name[] ) only the adress of the array name is copied instead of the whole array. It is a C\C++ feature.

Furthermore the name defined as char name [20] is not a modifiable lvalue.

Regarding strcpy: it will bring undefined behaivour as if your source array doesn't have a NULL character it will copy some trash to this->name too. You may read more about strcpy here

查看更多
男人必须洒脱
5楼-- · 2019-01-20 01:22

If you still want to assign array to array, use a loop.

for eg: class UCSDStudent {

char name[20];

public:

UCSDStudent( char name[] ) 
{

for(int i = 0; i<20; i++)
  {
  this-> name[i] = name[i];

  }
}
};  
查看更多
倾城 Initia
6楼-- · 2019-01-20 01:24

Because you assingned name as an array so you can not copy just like that and there is 21 count which name has. You must use "=" to copy an with loops and you have to identify array's count.

 for(int i = 0; i<20; i++){

  this -> name[i]=name[i];   
  }
查看更多
登录 后发表回答