This is because your piece of code does not execute the declaration, but the assignment:
char name[10]; // Declaration name= "Rajesh"; // Assignment.
And arrays cannot be directly assigned to C.
The name
actually resolves the address of its first element ( &name[0]
), which is not an lvalue , and how that cannot be the purpose of the assignment.
Declarations and Assignments of String Variables
String variables can be declared in the same way as other arrays:
char phrase[14];
String arrays can be initialized or partially initialized simultaneously with the declaration using the list of values enclosed in brackets {{} "(the same applies to arrays of other data types). For example, the statement
char phrase[14] = {'E','n','t','e','r',' ','a','g','e',':',' ','\0'};
both declare an array of phrase and initialize its state. Statement
char phrase[14] = "Enter age: ";
equivalent to. If "14" is omitted, the array will be created large enough to contain both the value "Enter age:" and the sentinel character "'\ 0", so there are two operators
char phrase[] = {'E','n','t','e','r',' ','a','g','e',':',' ','\0'}; char phrase[] = "Enter age: ";
equivalent to each other and to the statement
char phrase[12] = "Enter age: ";
However, it is important to remember that string variables are arrays, so we cannot just assign and compare using the "=" and "==" operators. We cannot, for example, just write
phrase = "You typed: "; //Wrong way
Instead, we can use a special set of functions for line assignment and comparison.
Edited by:
And in another way, do this using a pointer: -
Declare a variable
char const *phrase;
And initialize the variable as you want, as
phrase = "Test string";