3 Replies - 329 Views - Last Post: 31 December 2012 - 06:50 PM Rate Topic: ****- 1 Votes

#1 ashokkumar01cbe  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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  Icon User is offline

  • member icon


Reputation: 3987
  • View blog
  • Posts: 12,300
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • 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 

Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,531
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1