-
April 2nd, 2009, 02:49 PM
#1
string - char* mix and match??
After 3 months of 'C++' (previous languages did not include 'C') I think it's great. But I still find the 'C' bits and the 'C++' bits trip me up.
A simple example is that I generate a format string, as a string type, only to discover printf rejects the string and wants a char*.
My questions:
Is the above true or am I just missing some simple conversion?
Should I try to mix them (C and C++) to the minimum or is the marriage simply the way it is?
String types just seem natural and char* so alien.
Cheers Nigel
-
April 2nd, 2009, 02:53 PM
#2
Re: string - char* mix and match??
Originally Posted by nigelhoath
only to discover printf rejects the string and wants a char*.
actually printf wants a const char * for its format string.
That conversion is simple
Code:
std::string fmt("%s");
printf(fmt.c_str(), "something" );
Kurt
-
April 2nd, 2009, 03:18 PM
#3
Re: string - char* mix and match??
A simple example is that I generate a format string, as a string type, only to discover printf rejects the string and wants a char*.
Really, you should try as much as possible to avoid using the printf family of functions in C++ anyway. They aren't typesafe and can only be used with built-in types. As much as possible you should prefer to use stringstreams or, for cleaner more printf like syntax, use boost::format.
-
April 2nd, 2009, 03:19 PM
#4
Re: string - char* mix and match??
I am a newbie to C++, I have summed up my understanding (correct me if I am wrong)
*** printf is mainly used in C, so its best to avoid using printf
Its good to to know 2 basic things:
1) char is a built in data type - It stores as an array of characters terminated by '\0'. So always treat it like an character array.
2) string - It is a class defined in the STL library, so treat it like a class, and it has many operators overloaded for convenience.
I have summed up my understanding of char and strings:
char
-----
- char is a built in data type
- char array is capable of storing a string, each element of the char array contains one character
- The end of the string is marked by the null character '\0'
- The size of the char array has to be one bigger than the length of the string it is going to store
- Like any other array, it starts with the index 0, and other properties of an array are applicable (such as can't be compared / assigned directly, will have to be compared / assigned element by element of the array or can use the char functions such as strcmp or strcpy)
- Remember to use '' when you assign values to every element of the char array.
- An example:
case1:
-------
char a[6] = "Hello"; //This is valid, because this is initialization
case2:
-------
char a[6] = { 'H', 'e', 'l', 'l', 'o', '\0'}; // This is the same as above
case3:
--------
char a[6];
a = "Hello"; //This is invalid, because this is an assignment and not initialization, and when you want to assign you have to go element by element or use strcpy
a[0]='H'; //valid
String
--------
- Always remember this is a class and treat accordingly.
- This has many operators overloaded for convenience
- Assignment is legal
- when you declare an object (variable) against this class, the object is capable of storing a string of any size, you don't have to worry about the size
- It also has a set of functions that can be invoked.
string a;
a = "Hello"; //Assignment is valid
cout << a.length(); //objectName.functionName()
Last edited by Muthuveerappan; April 2nd, 2009 at 03:24 PM.
-
April 2nd, 2009, 04:34 PM
#5
Re: string - char* mix and match??
If you want to use C++, don't use printf, use C++ idioms.
boost::format, boost::lexical_cast, and tried and true stringstream (here is sample of stringstream)
C does not offer type safety, where C++ (the one I mentioned) have type safety. If you don't grok that, you will in the future.
This http://www.codeguru.com/forum/showth...35#post1807435 post has a little on boost if you are not familiar.
Also, std::string do provide const char *, you use std::string::c_str() member.
Last edited by STLDude; April 2nd, 2009 at 04:35 PM.
Reason: Added c_str
-
April 2nd, 2009, 04:39 PM
#6
Re: string - char* mix and match??
Many thanks STLdude. I'll chase those down. Never heard of boost so I guess that will get me back on strings track.
Yup I'd like to stick to C++. I find it better after each prog.
-
April 4th, 2009, 04:50 AM
#7
Re: string - char* mix and match??
[ merged threads ]
Please do not start multiple threads on the same issue.
-
April 4th, 2009, 07:55 AM
#8
Re: string - char* mix and match??
Originally Posted by nigelhoath
After 3 months of 'C++' (previous languages did not include 'C') I think it's great. But I still find the 'C' bits and the 'C++' bits trip me up.
A simple example is that I generate a format string, as a string type, only to discover printf rejects the string and wants a char*.
That's kind of odd.
If you had no previous knowledge of C, and going straight to C++,
at what point did you come across the printf?
-
April 4th, 2009, 08:52 AM
#9
Re: string - char* mix and match??
Hello Muthuveerappan
Originally Posted by Muthuveerappan
Its good to to know 2 basic things:
1) char is a built in data type - It stores as an array of characters terminated by '\0'. So always treat it like an character array.
I think it's good to clarify that char itself is not an array. It can only hold a single char. Not all char arrays are null terminated. I think what you're refering to is the C-style character strings (null terminated char array). Another good thing to keep in mind is that string literal is an array of const char.
Last edited by potatoCode; April 4th, 2009 at 08:54 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|