#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    1
    Rep Power
    0

    Question Can c++ strings be used as char buf[]?


    I'm playing around with some sockets and most of the c based commands utilise char buf[BUFSIZE]. I've been using c++ strings and sending &string[0] whenever a char * is required. I'm aware that a buf can be a string or not depending on wether is is null terminated but is it a bad idea to force a c++ string object into a char buf[]?



    :rolleyes:
  2. #2
  3. Shes dancing (obviously)
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2002
    Location
    the far side
    Posts
    527
    Rep Power
    13
    why dont u just convert it out of the C++ string to a C string?

    microsofts butterfly is their way off telling u their systems have a **** load of buggs
    Advocating Linux Guide
    Lesbian Linux
    Great & Practical Computer Books

    like the links?
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    It is a bad idea only if the function alters the char buffer. This is because, the string object may keep the size of the string allocated in a different member of the class (depending on the implementation of course), and if the function alters the char buffer, interesting things might occur later on. If the function accepts a const char* instead of a char *, then at least you have some assurance that the function is not going to alter the contents. BTW string objects have a c_str() member function which returns the pointer to the char array of the object.
    Last edited by Scorpions4ever; March 15th, 2003 at 07:15 PM.

IMN logo majestic logo threadwatch logo seochat tools logo