What is the different between string and char* in C++

Pages: 12
Hi, everyone.
I tried this following code and there is nothing appear:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string firstStr = "This is first string";
    string secondStr;
    
    for (int i = 0; i < 20; ++i)
        secondStr[i] = firstStr[i];
        
    cout << secondStr;
    
    return 0;
}

So can you explain it for me?
Last edited on
Actually, something does appear. It just disappears too fast for you to see it.

Add cin.get() just before return 0, and that should fix part of your problem.

-Albatross
Last edited on
@Albatros: Thank you very much.
I tried as you said but there is still nothing appear.
Extra insignificant detail that is very significant:
http://cplusplus.com/reference/string/string/operator[]/

You can't use that operator for the purposes of copying strings. Silly me.
Use copy or replace?

Stupid caffeine dependency...

-Albatross
Last edited on
http://cplusplus.com/forum/beginner/1988/
Read the above thread on keeping your console window open.

As for copying first str to secondStr, all you need to do is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string firstStr = "This is first string";
    string secondStr = firstStr;
    
    //for (int i = 0; i < 20; ++i)      //not needed since string class allows you to assign a string to
    //    secondStr[i] = firstStr[i];  //another string variable
        
    cout << secondStr;
    
    return 0;
}
@nammae
Hi,
I think if we will modify code a bit, it will work,
just like

1
2
secondStr=firstStr[i];
cout<< secodStr << " "; //will show the result with a space after every output 



Regards
closed account (Lv0f92yv)
Also, to bring up the point the title entails:
What is the different between string and char* in C++


A string is a sequence of characters. A char* myChar declaration declares myChar to point to one (or more) characters, depending on its usage.

Consider the following example:

1
2
3
4
5
char* a;
	string str = "test";
	a = &str[0];
	cout << a;
	getchar();


Line 3 assigns the address of the first character in the sequence of characters 'str' to my pointer, 'a'. Printing 'a' will print that character (the first of the sequence), as well as all others until the null terminator char, '\0'.

Now see:

1
2
3
4
5
 char* a;
	string str = "test";
	a = &str[1];
	cout << a;
	getchar();


Note that part of the 'str' is cut off. This is because 'a' now references the character after the first character in the str. (The offset of 1, not 0).

Now, we can try:

1
2
3
4
5
char* a;
	string str = "test";
	a = &str[0];
	cout << a[1];
	getchar();


where cout << a[index] prints the character in the array of characters at position 'index'.

These examples may not have been as clear as they come, but I hope they help.
That doesn't say that much. I can replace the word "string" in each of those snippets with "char *" and the code behaves exactly the same. It would have been better to say that an std::string is an object that abstracts string handling, while a char * is just a pointer to a char or an array of chars.
Last edited on
@Everyone: Thanks for your helps.
I know we can make this program run by using secondStr = firstStr;
But as you can see in the title of this topic, I wonder what is the different between char * (or char []) and string in C++.
And I want to know why we can't do as the following code:
1
2
for (int i = 0; i < firstStr.length (); ++i)
        secondStr[i] = firstStr[i];

I also tried this statement: cout << secondStr.length (). The input is 0.
Why?
@nammae
Hi again, use this definition of secondStr.

string secondStr(20, ' ')

and don't change anything else. It will work.

Regards
Not very sure but it looks like the default constructor of string initializing the variable with single empty string and this is the cause of problem. I don't have that much idea yet. Just assuming.
How about using the += operator on an empty string?

-Albatross
Not very sure but it looks like the default constructor of string initializing the variable with single empty string and this is the cause of problem. I don't have that much idea yet. Just assuming.
High quality posting, coming through!
closed account (Lv0f92yv)
Thanks for the clarification helios.

secondStr[i] = firstStr[i];


Won't work because strings are immutable - they cannot be modified once created (as this attempts to change part of the string). A char array would allow for this.
Really I don't see any difference between char* and string
std::strings are mutable. In fact, all objects are mutable unless declared const.
Last edited on
closed account (Lv0f92yv)
They are? I was under the impression the string class represents an immutable object... I know this is how it is in Java and (according to wikipedia, the .NET framework), but not C++?
No, of course not. That would be a horrible mismanagement of resources.
@Desh,

I think it is working if we will define secondStr as string of 20 char. As I have told, why is it working not firs one I don't know.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string firstStr = "This is first string";
    string secondStr(20, ' ');
    
    for (int i = 0; i < 20; ++i)
        secondStr[i] = firstStr[i];
        
    cout << secondStr;
    
    return 0;
}
No, of course not. That would be a horrible mismanagement of resources.


You are wrong! Strings are in fact a immutable class. No method of the string class changes it's content. Assignment operators excluded.

And i also believe that rajroshi solution should work, but what's the point of doing that if you could use the operator=, as pointed out before.
Pages: 12