Is there any way to change the value of the variable char[] ?

Example:

 char a[80]; if (b=d) a="qwerty"; else if (o=f) a="poiuyt"; 
  • 2
    it is possible) but you'd better use string . - pavel

2 answers 2

The variable a is an array, and the literal "qwerty" is an array. And it is impossible to appropriate arrays.

Therefore, it is necessary, for example, to make a pointer, i.e. const char* a; ,
or copy arrays with strcpy

 char a[80]; if (b == d) strcpy(a, "qwerty"); 

However, operations with ordinary arrays are fraught with buffer overflows, so it is better to use std::string .

  • one
    The philosophical question arises, by the way: what is the “value of the char [] variable” - a pointer to a place in memory where the array is located (a itself), or values ​​in the corresponding places in memory? Because the answer to the first question is categorically negative, changing the value of the variable a impossible :) - Harry
  • 2
    @Harry, this is not a philosophical question, type a is char[80] . - Abyx

All information that interests you here .

UPD

The first way:

 char a[50]; strcpy(a, "Hello World!"); 

The second way:

 char a[50]; char b[13] = {"Hello World!"}; for(int i(0);i<13;i++) { a[i] = b[i]; } 
  • Although the link can find the answer to the question, it is better to point out the most important thing here, and give the link as a source. If the page to which the link leads will be changed, the response link may become invalid. - From the queue of checks - Streletz
  • What zaminusovali ?? There are many ways to change the character array! I brought the link, let the author choose which method he needs! Gave 2 simple examples and added a message - Duracell
  • @AK - now is the answer to the question? - Duracell
  • @Streletz - fixed, remove the minus. - Duracell
  • And, I did not minus at all. On remarks from the queue of checks you are offended in vain. In addition to the author, the topic can be read by any user of the network. And there will be no benefit from answering with a link to a page that has "moved" or deleted. The fact that you corrected the answer is good. The only thing that corrected the code a bit (it is better to format it all the same). - Streletz