When to use char[] and when to use string
When to use char[] and when to use string
Hi all,
I find it easier mostly when working with strings to #include <string>
and set up a string like this
string test;
Also I find string manipulation easier with this, but a lot of inbuilt functions and libraries need to use char instead.
I sometimes spend longer trying to work this out rather than doing the program itself
I know you setup char with
char test[50];
Trouble with using char is that you need to know how many character a string has even before it is used, wheras with string method you don't need to know this.
My question is. When do I use char or string?
I find char cumbersome compared to strings, but so many things need char.
An explanation would be really useful.
Also ways to convert from char to string and vica versa, and is it wise to?
Asimov
I find it easier mostly when working with strings to #include <string>
and set up a string like this
string test;
Also I find string manipulation easier with this, but a lot of inbuilt functions and libraries need to use char instead.
I sometimes spend longer trying to work this out rather than doing the program itself
I know you setup char with
char test[50];
Trouble with using char is that you need to know how many character a string has even before it is used, wheras with string method you don't need to know this.
My question is. When do I use char or string?
I find char cumbersome compared to strings, but so many things need char.
An explanation would be really useful.
Also ways to convert from char to string and vica versa, and is it wise to?
Asimov
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk
Re: When to use char and when to use String
I think they are pretty much the same. char* is like and array, which you have to define in advance where string is allocated and can therefore have any size, like shown in chilis tutorial 22. That's how I understood it.
There's a way to convert them, I think I used it in my space shooter, but I can't remember it anymore. Quick google should do.
There's a way to convert them, I think I used it in my space shooter, but I can't remember it anymore. Quick google should do.
ʕ •ᴥ•ʔ
Re: When to use char and when to use String
In general it is better to use std::string. I will be doing that later (not much later) on. The main reason I haven't so far is because a: it's good to know how c-strings work; and b: it's good practice for learning how to work with pointers, arrays, etc.
Chili
- npissoawsome
- Posts: 114
- Joined: June 8th, 2012, 3:01 pm
Re: When to use char and when to use String
Strings are slower though :pchili wrote:In general it is better to use std::string. I will be doing that later (not much later) on. The main reason I haven't so far is because a: it's good to know how c-strings work; and b: it's good practice for learning how to work with pointers, arrays, etc.
And you can easily convert them
Code: Select all
std::string str = "Hello";
char cstr[] = str.c_str();
Code: Select all
char[] cstr = "Hello";
std::string = cstr;
Re: When to use char and when to use String
Hi npiss,
Yeh I found this information a while a go and lost it heh heh, thanks.
Asimov
Yeh I found this information a while a go and lost it heh heh, thanks.
Asimov
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk
- npissoawsome
- Posts: 114
- Joined: June 8th, 2012, 3:01 pm
Re: When to use char and when to use String
Coming back to this though, I think you should always be using strings, because whenever a function requires a char[] or char* you can just do stringname.c_str(), making it backwards compatible
Re: When to use char and when to use String
But can strings alter individual characters? Just asking.
Say, you have a textbox buffer in string. You have typed in "Hello ir LuX". Using strings is it possible to change the index 8 without having to write everything else, since with char* you can easily set charbuff[8] = "m". Or do I need to use some combine function that combines a specific location, or do I in such case need to combine a string '______m' to combine the right spot?
Say, you have a textbox buffer in string. You have typed in "Hello ir LuX". Using strings is it possible to change the index 8 without having to write everything else, since with char* you can easily set charbuff[8] = "m". Or do I need to use some combine function that combines a specific location, or do I in such case need to combine a string '______m' to combine the right spot?
ʕ •ᴥ•ʔ
- npissoawsome
- Posts: 114
- Joined: June 8th, 2012, 3:01 pm
Re: When to use char and when to use String
I googled it, and I'm not 100% sure, but I believe you can doLuX wrote:But can strings alter individual characters? Just asking.
Say, you have a textbox buffer in string. You have typed in "Hello ir LuX". Using strings is it possible to change the index 8 without having to write everything else, since with char* you can easily set charbuff[8] = "m". Or do I need to use some combine function that combines a specific location, or do I in such case need to combine a string '______m' to combine the right spot?
Code: Select all
stringname[#] = "x";
Re: When to use char and when to use String
You can do this:
Note: it has to be 'r'; "r" is no good.
Code: Select all
std::string myString = "We're having an election!";
myString[ 17 ] = 'r';
printf( myString.c_str() );
Note: it has to be 'r'; "r" is no good.
Chili