CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2009
    Posts
    48

    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

  2. #2
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: string - char* mix and match??

    Quote Originally Posted by nigelhoath View Post
    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

  3. #3
    Join Date
    Aug 2007
    Posts
    858

    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.

  4. #4
    Join Date
    Feb 2009
    Posts
    326

    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.

  5. #5
    Join Date
    May 2007
    Posts
    811

    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

  6. #6
    Join Date
    Mar 2009
    Posts
    48

    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.

  7. #7
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: string - char* mix and match??

    [ merged threads ]

    Please do not start multiple threads on the same issue.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  8. #8
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: string - char* mix and match??

    Quote Originally Posted by nigelhoath View Post
    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?

  9. #9
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: string - char* mix and match??

    Hello Muthuveerappan
    Quote Originally Posted by Muthuveerappan View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured