Programming Lines c

why can't i compile a program containing code

char name[10]; name= "Rajesh"; 

While I can compile the program with

 char name[10]="Rajesh"; 
+4
source share
6 answers

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; /* a pointer to type character */ 

And initialize the variable as you want, as

 phrase = "Test string"; 
+9
source

You cannot assign values ​​to string arrays using assignment.
In C, you can only initialize arrays without assigning them; an array of characters is no exception to this rule.

You will need string copying functions like strcpy or strncpy etc.

However, you can encapsulate the string in the structure and mimic this:

 typedef struct Yourstring Yourstring; struct Yourstring { char a[24]; }; Yourstring a = { "abcd" }; Yourstring b = a; Yourstring c = { 0 }; c = b; 
+5
source
 char name[10]; 

In this first example, you declare name array of ten characters. The name character is now interpreted as the starting address of this array, but while you can write to the array, you cannot move the name character. So this is:

 name= "Rajesh"; 

it would mean specifying name away from the declared array and in the place of the string literal "Rajesh" , which is stored elsewhere in memory. You just can't do it.

What you can do either:

 strcpy(name, "Rajesh"); 

which copies the string literal from an immutable location in your executable file, to the char array you declared, or:

 char const *pointer_to_name = "Rajesh"; 

which does not copy anything, but simply stores the address of your immutable string literal into a variable in which you can use it, or your second example:

 char name[10]="Rajesh"; 

which declares name array of 10 characters and initializes it.

+3
source

AS char name[10]="Rajesh" is a definition compiler that understands what you are trying to do and corrects your error. In C ++, lines written in "" are persistent, and some compilers put them in empty disks to save space. name="...." means that you are trying to assign a constant to a volatile pointer that is not allowed.

you must use strcpy to copy the string to the array.

0
source
 char name[10] ="Rajesh"; 

This parameter is the initialization of the array. The compiler knows about this. This is a one-time trick. You can use it only when defining a variable. This would be equivalent to:

 char name[10] = { 'R', 'a', 'j', 'e', 's', 'h', '\0' }; 

The other is illegal, because you cannot use array initialization outside the array definition .

0
source

I don’t remember exactly where I read it, but, as the C-standard says, you can assign a string value to an array during defination, but not after defination.

 char a[10]="rajesh" its a defination hence works char a[10];a="rajesh"; fails its not a defination 

rather, you need to use strcpy (a, "rajesh") to determine the value for the string if it is not defination

0
source

All Articles