Char* string: How To??

Started by
24 comments, last by Zakwayda 17 years, 6 months ago
Quote:Original post by TheUnbeliever
int myarray[5]
, all that does is reserve 5 * sizeof(int) bytes on the stack, and gives you myarray as a pointer to that bit of memory. You can then use *myarray interchangeably with myarray[0].


This is not perfectly true. An array in C is not a pointer, as can be seen from the low-level operations necessary to access one of its elements. To access element i in array a, add i to a and get the value there. To access element i in a buffer pointed to by p, get the value of p, add i to it, and get the value there.

A stack array requires one less memory access than a pointed-to buffer. Of course, this is usually incompatible with argument passing, so stack arrays decay into pointers. But they are not pointers.

Back to the topic: use std::string. You don't need char* in C++ to manipulate strings, and you apparently do not have the required level of experience to get reasonable performance out of char* anyway.
Advertisement
Quote:Original post by Emmanuel Deloget

The other people are right, but this doesn't disallow you to use this litteral constant in your code. Remember that it's type is not "char*" but "const char*" and that you should not modify it by any mean and you're quite safe.

Actually, no, you are not that safe. As pointed out by both our lovely Zahlman and our dedicated Enigma, in C++ you should use std::string and its methods, not char* strings and a bunch of unsecure and obsolete C functions.

I repeat it and underline it: use std::string.

(There is a missing C++ by the way. C doesn't have std::string [smile])


It's ok, I got it :D
I understood, I just wanted to know a bit more .. just for culture.

I'll use arrays, just like text1 and text2 in nobodynews reply. Thanks again.


PS: Emm, does anybody know where I can find any std::string tutorials? I've looked on google but, since I live in france.. it gets to google.fr and I can't find much.. ^^
Good thread. The same thing was repeated about 10 times by 10 different people. Sometimes that's what it takes though I guess.

Your example in the standard library:

#include<iostream>#include<string>int _tmain(int argc, _TCHAR* argv[]) {    std::string inputText= "BlaBla XXBlaBlaXXBlablaXXBlablabla";    std::string::size_type pos=0;    std::string::size_type old=0;    while((pos=inputText.find_first_of("XX",old))!=std::string::npos) {	std::string linestring=inputText.substr(old, pos-old);	std::cout << linestring << '\n';	old=pos+2;    }    if(old!=inputText.size())	std::cout << inputText.substr(old, inputText.size()-old) << '\n';    return 0;}


That's one way of doing it. It just continually searches your string and tokenizes it.

If you like the standard libarary, you can then look into the boost library which has a tokenizer class that might do all of this for you (although I've never used it.)

[Edited by - Absolution on October 19, 2006 3:42:30 PM]
Ok, one thing and then I'm done:
In my program, instead of outputing the string with cout I need to use a the OpenGL renderFont function;

Point is it only accepts a char* value;

So, to "convert" the string I tried to use .c_str();

Assuming that inputText is a std::string that's what I wrote:
font->RenderFont(0, 0, font->m_fontListBase, inputText.c_str());

If I use this function I get an error saying: "Invalid conversion from const char* to char*"

It smells like this has also been answered a million times.. ..I'm sorry ^^.

Problem Solved !!!
Changing the value type renderFont accepts from char* to const char* worked.

Again, thanks a lot for you patience and sorry for my ignorance.
Merlino
You can also cast away constness with const_cast<char*>(inputText.c_str()).

Edit: forgot the .c_str().

[Edited by - Absolution on October 19, 2006 6:21:13 PM]
Quote:Original post by Absolution
You can also cast away constness with const_cast<char*>(inputText.c_str()).
You can, but in most cases you shouldn't.

In any case, since the RenderFont() function is apparently part of the OP's own code, making the argument const is a far better solution than casting the constness away.

This topic is closed to new replies.

Advertisement