Thread: Appending to the end of a char*

  1. #31
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's a type. The size() member function of strings return a size_t.
    Though, it could probably be written as
    size_t tempsize = str.size();
    The size_t(...) part is a cast.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #32
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by thegr8n8
    what is teh size_t?
    An unsigned integer type that is the type of the result of the sizeof operator. It is a natural choice for a variable that stores the size of an array. If you use a std::vector then you do not need another variable to store the size of the dynamic array since the vector keeps track of its own size.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #33
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It's a type. If you look at string's copy() function, you'll see why I used it. It's because copy uses it. C++ has a strict type system, so you want to avoid using the wrong type if you can. If you're careless, you're also, by necessity, very comfortable with type promotion rules, which will change the types of certain variables used in expressions that aren't the same type as the result.

    For example:

    size_t tempsize = size_t(str.size());

    Here I elected to use an explicit type conversion because the return value of size() is not size_t, but string::size_type. Perhaps more correct would be static_cast<size_t> (str.size()); but there are a lot of things to explain in that statement. For starters. There's no reason to believe that string::size_type isn't convertible to size_t though...

  4. #34
    Banned
    Join Date
    Aug 2010
    Location
    USA! USA! USA!
    Posts
    19
    okay here is a header of one of the functions to that networking engine:
    Code:
    double writestring(char*str, double buffid)
    {
    	CBuffer*buff = (CBuffer*)buffers.item((int)buffid);
    	if(buff == NULL)return 0;
    	return buff->writestring(str);
    }
    Its using char* because its communicating with another program that only accepts doubles and char*

  5. #35
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well to sum up, you could use vector<char> or what I showed you to do this.

    If my example or any of the others are confusing, then what you're doing is:

    1. Constructing something array like from the string object.
    2. Calling your function by passing the address of the first element, which is the same type as char*
    3. Cleaning up the array like object if necessary.

    From there you continue to use standard strings.

    This modularizes the code, so that you're only using lower-level stuff when you call on the networking API.

    Now in the interest of full disclosure, laserlight is correct about my example and its lack of exception safety. vector, and just about anything else, would be safe, but writestring() doesn't throw unless buffers.item() throws. Maybe nothing else does either. I'm not clairvoyant.

  6. #36
    Banned
    Join Date
    Aug 2010
    Location
    USA! USA! USA!
    Posts
    19
    okay lets put this char* thing on hold. Lets move towards a diffrent function I have been working on.
    Code:
    int stringToInt(string paraString)
    {
        return atoi (paraString.c_str());
    }
    Is this the most EFFICIENT way to do this?

  7. #37
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I can't say about efficient, but there are several ways to do it. Two are:
    Code:
    int n = boost::lexical_cast<int>(mystr);
    Another is:
    Code:
    int n;
    std::stringstream sstr(str);
    sstr >> n;
    If you're going to make a function, consider passing the string by const reference. Otherwise you'll hurt efficiency by making a copy.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #38
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, it would likely be more efficient to write:
    Code:
    int stringToInt(const string& paraString)
    {
        return atoi(paraString.c_str());
    }
    But then if paraString contains some invalid character, this would also fail to detect it. If that is acceptable, then you're okay, otherwise use a stringstream method or strtol.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #39
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well atoi is pretty fast. If paraString is not guaranteed to be an integer and an integer in the range of int, you'll have to verify the conversion. Again, string streams will work. Just test the stream state after you extract the int.

  10. #40
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Depends on your code flow too, I suppose. boost::lexical_cast will throw an exception if it fails. But don't worry about speed unless you figure out that it's really a bottleneck.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #41
    Banned
    Join Date
    Aug 2010
    Location
    USA! USA! USA!
    Posts
    19
    why did you change it to a const?

  12. #42
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To make sure the function doesn't modify it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #43
    Banned
    Join Date
    Aug 2010
    Location
    USA! USA! USA!
    Posts
    19
    lol, the function is only 1 line though

  14. #44
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, but then again, const correctness is like a chain. Once you begin the chain, you can't break it. A const function cannot call non-const functions. And a function that takes arguments by const reference cannot pass these into functions expecting non-const arguments.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #45
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    laserlight changed it to a const reference because making a copy is slower than not making a copy. If it was left as a normal reference you wouldn't protect the original data.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM
  4. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 02:40 PM
  5. appending some letters to the end of a CString variable.
    By Eber Kain in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2001, 03:00 PM