19

I get no error when I type

char a[10] = "Hi";

But when I change it to the following, I get an error: Array type char is not assignable.

char a[10];
a = "Hi";

Why is array type char not assignable? Is it because the language is written that way on purpose or am I missing a point?

Rutvij Kotecha
  • 935
  • 2
  • 10
  • 21
  • For responses: Since this is tagged C++, is there a *non*-strcpy "more C++" way to do this while dealing with `char[]`? –  Feb 16 '13 at 22:56
  • A "more C++ way" would be to use string or vector instead of char[]. By using char[] you're basically accepting all the C-ness that goes with it. – Cogwheel Feb 16 '13 at 23:07
  • Nominated to reopen because this is tagged c++ and the "duplicate" says "in C". I know the answer is the same either way, but they're still different languages and the answer is subject to change or have subtleties worth pointing out here. – Jim Hunziker Feb 16 '18 at 15:31

7 Answers7

27

An array is not a modifiable lvalue

use

char a[10];
strcpy(a, "Hi");
learnvst
  • 15,455
  • 16
  • 74
  • 121
15

The C++ way of doing this, as I commented above, would be to use std::string instead of char[]. That will give you the assignment behavior you're expecting.

That said, the reason you're only getting an error for the second case is that the = in these two lines mean different things:

char a[10] = "Hi";
a = "Hi";

The first is an initialization, the second is an assignment.

The first line allocates enough space on the stack to hold 10 characters, and initializes the first three of those characters to be 'H', 'i', and '\0'. From this point on, all a does is refer to the position of the the array on the stack. Because the array is just a place on the stack, a is never allowed to change. If you want a different location on the stack to hold a different value, you need a different variable.

The second (invalid) line, on the other hand, tries to change a to refer to a (technically different) incantation of "Hi". That's not allowed for the reasons stated above. Once you have an initialized array, the only thing you can do with it is read values from it and write values to it. You can't change its location or size. That's what an assignment would try to do in this case.

Cogwheel
  • 22,781
  • 4
  • 49
  • 67
  • 1
    Would you mind including that you could use a `std::string` and get the behavior the OP wants? I think that would be a good addition to a good answer. – NathanOliver Mar 11 '16 at 16:25
  • 1
    Yeah, I usually try to give two tier answers to questions like this (one direct, the other second-guessing the intent). Since the asker had seen my comment and this thread got closed as dupe, I guess I didn't bother. – Cogwheel Mar 11 '16 at 17:29
12

The language does not allow assigning string literals to character arrays. You should use strcpy() instead:

strcpy(a, "Hi");
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 1
    Correct -- for a situation like this you should use `strcpy`. Forgetting you ever even heard of `strncpy` would probably be no loss at all -- it's almost as useless as `gets`. – Jerry Coffin Feb 17 '13 at 00:37
  • Related [Difference between 'strcpy' and 'strcpy_s'?](http://stackoverflow.com/questions/32136185/difference-between-strcpy-and-strcpy-s) – Quazi Irfan Mar 03 '17 at 05:45
2

a is a pointer to the array, not the array itself. It cannot be reassigned.

You tagged with C++ BTW. For that case better use std::string. It's probably more what you're expecting.

1

Simple, the

char a[10] = "Hi";

is a little "extra feature", as it cannot be done like that on run-time.

But that's the reason for C/C++ standard libraries.

#include <string.h>

// ...
strcpy(a,"Test"); // STR-ing C-o-PY.

This comes from the C's standard library. If using C++ you should use std::string, unless you really want to suck all the possible performance from your destination PC.

SGH
  • 65
  • 6
0

this is because initialization is not an assignment. the first thing which works is an initialization, and the second one, which does not work, as expected, is assignment. you simply cant assign values to arrays you should use sth like strcpy or memcpy. or you can alternatively use std::copy from <algorithm>

Hayri Uğur Koltuk
  • 2,970
  • 4
  • 31
  • 60
-2

It is so simple,(=) have two different mean assignment and initialization. You can also write your code like that

         #include <iostream>

         using namespace std;

          int main ()
         {
             char message[3] = {'H', 'i', '\0'};

                cout << message<< endl;

           return 0;

           } 

in this code you have no need to write a difficult code or function and even no need of string.h