Char Array

I am making a program in which i need to assign a string to a char array but when i do so it flags an error - "lvalue required".

For example
1
2
3
4
5
6
7
#include<iostream.h>
 #include<conio.h>
  
 void main()
 {
   char A[10];
   A="Hello";]


In doing so what's wrong??? And what can i do about it?
Last edited on
Why are you using a char array when you seem to want a string?
strcpy(A, "Hello"); is what you need. You can't assign to arrays like that, except when you initialize it.
Also, if you're writing c++ code (even though it seems c), you can use a string class which would allow assignment and lots of other neat things.
closed account (zwA4jE8b)
you have a ] on line 7 where it should be a }

Also A just references the base address of the array. you can do
char A = "Hello" then A will be a lenght 6 null terminated char array.
Last edited on
That would have to be char A[] = "Hello";.
A = "Hello";

while executing this line compiler create a const char array and return address of the first char.

so,
if A is a char * and your declaration is something like
char *A = "Hello";
then it work else it is wrong.

but if write char A[6] = "Hello";
Then compiler implicitly does the initialization as what you achieve by running the code below
A[i] = 'H';
A[i] = 'e';
A[i] = 'l';
A[i] = 'l';
A[i] = 'o';
A[i] = '\0';
Topic archived. No new replies allowed.