# why a string cannot assigned to the another string

Page 1 of 1

## 3 Replies - 445 Views - Last Post: 31 December 2012 - 06:50 PMRate Topic: 1 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=305210&amp;s=d6c3b251963b38fde594c19f13ff815f&md5check=' + ipb.vars['secure_hash'], cur_rating: 4, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ashokkumar01cbe

Reputation: -1
• Posts: 15
• Joined: 04-December 12

# why a string cannot assigned to the another string

Posted 30 December 2012 - 09:40 AM

```int main()
{
char str1[]="World",str2[]="Universe";
str1=str2;/*it will not work*/
}

```

today my professor taught me that we can't assign one string to another.I ask the question that it is possible to assign integer variable by another integer variable ,but why not we are able to do these in strings. But my professor told me that try to google it and find the answer. explain me..:confused:

Is This A Good Question/Topic? 0

## Replies To: why a string cannot assigned to the another string

### #2 jimblumberg

Reputation: 4565
• Posts: 14,417
• Joined: 25-December 09

## Re: why a string cannot assigned to the another string

Posted 30 December 2012 - 10:09 AM

Quote

today my professor taught me that we can't assign one string to another.

That is correct, you can't use the assignment operator to copy one string to another, you will need to use the strcpy() function.

Quote

I ask the question that it is possible to assign integer variable by another integer variable ,but why not we are able to do these in strings.

Because these "strings" are not the same as an int variable. You may want Google "C Character Arrays" as read the first couple of links.

Here is a good basic tutorial for Character Arrays.

Jim

### #3 buffalobill

Reputation: 21
• Posts: 188
• Joined: 08-July 08

## Re: why a string cannot assigned to the another string

Posted 30 December 2012 - 11:13 PM

If you use the standard C++ string type rather than C style string stored in a char array[] you can do pretty much what you are suggesting.
For example:
```#include<string>
string str1;//contains 0 characters
str2="Universe";//str2 contains 8 characters
str1=str2;//str1 and str2 each contain 8 characters
//You can get the length of a C++ type string with length().
cout<<str1.length()<<endl;//prints 8 ignoring the null char
```

### #4 jjl

• Engineer

Reputation: 1118
• Posts: 4,641
• Joined: 09-June 09

## Re: why a string cannot assigned to the another string

Posted 31 December 2012 - 06:50 PM

Quote

,but why not we are able to do these in strings.

C strings are nothing more than a character array, or a pointer to a character array.

An array is not a pointer, you cannot modify the base address of an array.

```   char str1[]="World",str2[]="Universe";
str1=str2;/*it will not work*/

```

So this assignment is directly attempting to change the base array address of str1 to str2 - therefore you get an illegal assignment error.

Here is the kicker, C strings can also be implemented by using a pointer to a character array. The following code creates character pointers that point to character arrays located on the string table. Remember that you can change where a pointer points to,

```char *str1 = "World", *str2 = "Universe";
str1 = str2; //Now str1 points to "Universe\0"

```

This post has been edited by jjl: 31 December 2012 - 06:50 PM