0

If I try to assign values to char array. It gives the error. Error '=': cannot convert from 'const char [5]' to 'char'.
Why the values assigned to char array are always constant?

class Employee
{
public:
    string name;
    char id[100];
    int age;
    long salary;

    Employee()
    {
        name = "NULL";
        id[100]= "NULL";
        age = 0;
        salary = 0;
    };
};

here i can't do assignment as "NULL" is considered as const char. while my id is char. why "NULL" is constant.

while if we individually check then we can change array values.

{
    char i[10]="maha";
    i[1]='z';
    cout<<i[0]<<i[1]<<i[2]<<i[3]<<endl;
}
  • You can't assign to arrays, and even if you could, `id[100]` is not an array but one `char`(which doesn't exist because the array only has 100 elements). Read some more about arrays in your favourite C++ book. – molbdnilo Jul 14 '20 at 04:28
  • https://stackoverflow.com/questions/12160233/assigning-char-array-a-value-in-c – R Sahu Jul 14 '20 at 04:28

2 Answers2

1

You're trying to fill maha in the 100th array index of id which is impossible. The array has only 100 indices, you're trying to access an out-of-range index.

Notice that you can initialize a char array during initialization but not during assignment of it.

char i[10] = "maha";

syntax is an initialization, whereas:

char id[100];
id[100] = "maha"; // incorrect

is an assignment.


There are two possible ways to solve it:

Method 1: Use of strcpy() -

strcpy(str, "maha"); // copying the string "maha" into 'legend'

Method 2: Use of pointer and memory allocation -

const int SIZE = 100;
char *str = new char[SIZE];

str = "maha"; // it's now modifable lvalue
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
  • Can you please explain the point that *str point to first element of array. So, if we are not incrementing it then how whole "maha" is assigned? Like *str adds on first index 'm' then how the next 'a' will be placed on next location –  Jul 14 '20 at 12:32
  • @LittleStar yes, I also admit that only the first character in the char array is pointed and also it get printed when dereferencing. But the memory for the rest chars 'a' 'h' 'a' were allocated one-after-another. – Rohan Bari Jul 14 '20 at 13:12
1

In your array total size is 100 (Index 0 - 99) and you are trying to set at index 100 which is out of range.

Here array is char means you can set only one character per index. you can not set id[10] = "maha".

You can initialize array by using memset function as below

memset(&id[0],0x00,sizeof(id))

Use memcpy to update array

memcpy(&id[10], "maha", 4)

Individual character update

id[10] = 'm'
id[11] = 'a'
id[12] = 'h'
id[13] = 'a'
jtro
  • 23
  • 4
  • How is that in array [100] i am assigning value to 100th index? Like my second code. In i[10] i have assigned "maha" so m is on 0th index not on 11th. –  Jul 14 '20 at 12:37
  • How is that in array [100] i am assigning value to 100th index? Like my second code. In i[10] i have assigned "maha" so m is on 0th index not on 11th. –  Jul 14 '20 at 12:39
  • You can change index by changing value in memcpy(&id[0], "maha", 4) or you can also use strcpy(&id[0], "maha"). When you initialize char array at that point value start from 0th index by default, After that you need to use memcpy or strcpy to update array. – jtro Jul 14 '20 at 17:04