Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning a string of characters to a char array

Tags:

c++

I Want to know why the first statements works and why not second one in c++

char a[10]="iqbal";  // it works

a="iqbal"; // does not work 
like image 971
Iqbal Khan Avatar asked Apr 10 '12 12:04

Iqbal Khan


People also ask

Can I assign a string to a char array in C?

The c_str() and strcpy() function in C++ C++ c_str() function along with C++ String strcpy() function can be used to convert a string to char array easily. The c_str() method represents the sequence of characters in an array of string followed by a null character ('\0').

How do you assign a character array?

Initializing Char Array A char array can be initialized by conferring to it a default size. char [] JavaCharArray = new char [ 4 ]; This assigns to it an instance with size 4.


4 Answers

Strictly speaking, an array is not a pointer! And an array ( base address of the array ) cant be a modifiable lvalue. ie it cannot appear on the left hand side of an assignment operator.Arrays decay into pointers only in certain circumstances. Read this SO post to learn when arrays decay into pointers. Here is one more nice article which explains the differences between arrays and pointers

Also read about lvalues and rvalues here so that you get an idea of things which cannot appear on the LHS of =

char a[10]="iqbal";  // it works

In this case, internally what happens is

a[0] = 'i';
a[1] = 'q'; 
 .
 .
a[5] = '\0';

So everything is fine as array[i] is a modifiable lvalue.

a="iqbal"; // does not work

Internally, this is roughly equivalent to

0x60000(Address of a, but is a simple number here ) = Address of "iqbal"

This is wrong as we cannot assign something to a number.

like image 69
Pavan Manjunath Avatar answered Oct 22 '22 07:10

Pavan Manjunath


The first line is not a statement but a declaration with an initialization. The second line is an expression statement with the assignment operator.

You cannot assign arrays in C.

But you can initialize an array with the elements of a string literal.

like image 26
ouah Avatar answered Oct 22 '22 09:10

ouah


why the first statements works and why not second one in c++

Because they are different statements, almost wholly unrelated. Do not be confused by the fact that they both use the = symbol. In one case, it represents object initialization. In the other case, the assignment operator.

Your first line is legal because it is legal to initialize aggregates, including character arrays.

Your second line is not legal because it is not legal to assign to an array.

Since this is C++, may I suggest that you avoid naked arrays? For character strings use std::string. For other arrays use std::vector. If you do, you example becomes:

std::string a = "iqbal";  // it works
a="iqbal"; // so does this
like image 37
Robᵩ Avatar answered Oct 22 '22 07:10

Robᵩ


The char array a will be static and can not be changed if you initialize it like this. Anyway you can never assign a character string a="iqbal" in c. You have to use strncpy or memcpy for that. Otherwise you will try to overwrite the pointer to the string, and that is not what you want.

So the correct code would do something like:

char a[10];
strncpy(a, "iqbal", sizeof(a) - 1);
a[sizeof(a) - 1] = 0;

The -1 is to reserve a byte for the terminating zero. Note, you will have to check for yourself if the string is null terminated or not. Bad api. There is a strlcpy() call that does this for you but it is not included in glibc.

like image 37
Albert Veli Avatar answered Oct 22 '22 08:10

Albert Veli