When to use char[] and when to use string

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

When to use char[] and when to use string

Post by Asimov » June 30th, 2012, 4:15 pm

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
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: When to use char and when to use String

Post by LuX » June 30th, 2012, 4:24 pm

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.
ʕ •ᴥ•ʔ

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: When to use char and when to use String

Post by chili » June 30th, 2012, 4:40 pm

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

User avatar
npissoawsome
Posts: 114
Joined: June 8th, 2012, 3:01 pm

Re: When to use char and when to use String

Post by npissoawsome » June 30th, 2012, 8:33 pm

chili 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.
Strings are slower though :p

And you can easily convert them

Code: Select all

std::string str = "Hello";
char cstr[] = str.c_str();
or

Code: Select all

char[] cstr = "Hello";
std::string = cstr;

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: When to use char and when to use String

Post by Asimov » June 30th, 2012, 9:51 pm

Hi npiss,

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

User avatar
npissoawsome
Posts: 114
Joined: June 8th, 2012, 3:01 pm

Re: When to use char and when to use String

Post by npissoawsome » July 2nd, 2012, 7:49 am

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

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: When to use char and when to use String

Post by LuX » July 2nd, 2012, 9:06 am

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?
ʕ •ᴥ•ʔ

User avatar
npissoawsome
Posts: 114
Joined: June 8th, 2012, 3:01 pm

Re: When to use char and when to use String

Post by npissoawsome » July 2nd, 2012, 9:17 am

LuX 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?
I googled it, and I'm not 100% sure, but I believe you can do

Code: Select all

stringname[#] = "x";
I guess the only drawback to using strings, is that they're slower

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: When to use char and when to use String

Post by chili » July 2nd, 2012, 1:35 pm

You can do this:

Code: Select all

std::string myString = "We're having an election!";
myString[ 17 ] = 'r';
printf( myString.c_str() );
:lol:

Note: it has to be 'r'; "r" is no good.
Chili

Post Reply