0

Look at Following code fragment

char ch[10] = "hello"; // is ok 

but

char ch[10]; ch = "hello"; // showing lvalue error 

It should have to be possible according to the concept that array decays into pointer.Why here ch is not decaying to char *? please describe the distinction between the two.

3
  • do you know what is lvalue? Commented Feb 23, 2014 at 11:57
  • Yes.these are those which takes space in memory and may be assignable or not Commented Feb 23, 2014 at 12:00
  • yes, lvalues are assignable values. In your case ch points to the address of first element of itself i.e. to its base address. So you can not change the base address of an array, whereas you can change the value at base address of array. Hence in this case ch is not a lvalue. See my answer for reference Commented Feb 23, 2014 at 12:03

6 Answers 6

2
char ch[10] = "hello"; 

is initialisation, and is allowed by the standard. It's effectively "give me a ten-char array and populate it with the C string "hello".

char ch[10]; ch = "hello"; 

is (attempted) assignment, and is not permitted. You can do:

char *ch; ch = "hello"; 

but then you end up with a ch that you're not allowed to modify (it's undefined behaviour as to whether or not you're allowed to modify string constants).

One way to get "assignment" happening with the array after initialisation time is:

char ch[10]; strcpy (ch, "hello"); 
Sign up to request clarification or add additional context in comments.

Comments

2

Arrays are not assignable. If you need to allocate a string dynamically then you either A) create a fixed length buffer beforehand and populate it, or B) allocate the memory dynamically and, again, populate it.

It all depends on whether or not you know the size beforehand (though C does support variable length arrays, but be careful) or if you need to accommodate a string of arbitrary length.

To copy a string into an existing buffer (allocated with automatic storage duration or otherwise) you can use strcpy, strncpy (for inserting into existing strings), strdup (POSIX), read it in with fgets, or whatever other method is appropriate. Be careful of buffer sizes and string lengths.

Comments

2

You should understand the difference between initialization and assignment. Initialization is defining a variable and assigning it a value in the same statement. The following statement defines a variable ch of type char[10] - an array of 10 characters and then assigns it with a string literal.

char ch[10] = "hello"; 

This is equivalent to

char ch[10] = {'h', 'e', 'l', 'l', 'o', '\0'}; 

Note that you assign only the first 6 elements of the array ch, the remaining 4 are implicitly initialized to the null character - '\0'. Therefore, the above two statements do the same as:

char ch[10] = {'h', 'e', 'l', 'l', 'o', '\0', '\0', '\0', '\0', '\0'}; 

An assignment is when you first define a variable and then assign it a value in a separate statement.

char ch[10]; // define ch of type char[10]. contains garbage right now. 

You should not use ch just after defining it because it contains garbage value. Next, you should assign it a value but then you can't assign it like you did in initialization. That's not allowed by the rules of C. You can't assign all elements of the array in one go but must assign each element of the array separately.

char ch[10]; const char *s = "hello"; int len = strlen(s); for(int i = 0; i <= len; i++) // copy the null byte as well ch[i] = s[i]; 

Here, you have assigned only the first 6 elements of the array ch, the remaining 4 still contain garbage values unless you assign them. That's one difference here between array initialization and assignment.

Comments

1

case 1 :-

char ch[10] = "hello";

Take the base address to be 1000

By your declaration

1000 -> h
1001 -> e
1002 -> l
1003 -> l
1004 -> o
1005 -> \0

And ch is pointing to 1000.

case 2:-

char ch[10];
ch="hello";

1000 -> garbage
1001 -> garbage
1002 -> garbage
1003 -> garbage
1004 -> garbage
1005 -> garbage
and so on

Note that hello is a independent string having its own address, so by ch="hello"

You are trying ch to point to the new address of h in "hello".

Which is impossible as ch is of type char *const.

I mean to say that ch will always point to its base address and you can not change the base address of an array.

You can change the values of an array, but not the addresses of array elements.

Comments

1

This

char ch[10]; ch = "hello"; 

is not valid in C. After you declared your array, you cant reassign it. The name of an array is actually the address of the first element of that array. ch is an array, hello is also array, it does not have name but it has it's own address, so this ch = "hello"; line, is an attempt to reassign ch with another address, which is not valid. You can only

strcpy(ch,"hello"); 

in order to put hello into it

Comments

0

ch = "hello" is not initialization, it's assignment. Also "hello" type and ch type is different.

Change it to char ch[10] ="hello".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.