# Char* string: How To??

This topic is 4475 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, here is my problem: I'm writing text on screen using OpenGL and I'm having problems with starting new lines. I wanted to write a code that tells the application to start a new line when it finds the string "XX"; Here is the code:
char * imputText = "BlaBla XXBlaBla";
char * reference = NULL;
reference = strstr(imputText, "XX");
if (reference == NULL) font->RenderFont(0, 0, font->m_fontListBase, imputText); //This writes the content of imputText at (0;0) on the screen;
else
{
font->RenderFont(0, 0, font->m_fontListBase, (reference + 2)); //This writes the content of (reference + 2) at (0;0) on the screen;
reference = '\0' //This should erase everything from the imputText string starting from the first X. Point is it does not work.
font->RenderFont(0, 30, font->m_fontListBase, imputText); //This writes the content of imputText at (0;30) on the screen;
};


As reference is a pointer I don't see why imputText is not modified if reference is modified... perhaps it is not... so this is my problem: if i render this i get: BlaBla XXBlaBla BlaBla whereas I would like to get: BlaBla BlaBla Do you know how to solve this?? Thanks in Advance, Merlino.

##### Share on other sites
char * imputText = "BlaBla XXBlaBla";
char * reference = NULL;
reference = strstr(imputText, "XX");
if (reference == NULL) font->RenderFont(0, 0, font->m_fontListBase, imputText);
else
{
font->RenderFont(0, 0, font->m_fontListBase, (reference + 2));
reference = '\0' //This should erase everything from the imputText string starting from the first X. Point is it does not work.
font->RenderFont(0, 30, font->m_fontListBase, imputText);
};

refrence should equal a pointer to the string "XXBlaBla"
so, it will enter the else statement
in the else statement, it should render "BlaBla"
then it will empty render to null
and then it should print inputtext, which is still pointing to "BlaBla XXBlaBla"..

So, in order it is printing
BlaBla
BlaBla XXBlaBla

(refrence+2)
(imputtext)

as a side note, you spent "input" wrong?

also
char * reference = NULL;
reference = strstr(imputText, "XX");

should be
char * reference = strstr(imputText, "XX");
(your compiler will likely make it this way logically anyway.
No sense calling the default constructor on refrence if you are only going to replace it.

##### Share on other sites
i wrote this:
BlaBla XXBlaBla
BlaBla

just because in the window space, inputText is higher than (reference+2).
(Sorry about the input !! :P)

But, isn't reference equal to (inputText+7) ?
That's what i wanted to do:
1 - write the second line, using reference without the XX. [font->RenderFont(0, 0, font->m_fontListBase, (reference + 2));]
2 - change the value of inputText from: "BlaBla XXBlaBla\0" to BlaBla \0" so that the second BlaBla is not rendered 2 times. [reference = '\0']
3 - write the first line, using inputText. [font->RenderFont(0, 0, font->m_fontListBase, imputText);]

Clearly, what does not work is reference = '\0' meaning that reference is modified but inputText is not. And I don't understand why...

Thanks.

##### Share on other sites
doesn't anybody know??

##### Share on other sites
Quote:
 Original post by MerlinoClearly, what does not work is reference = '\0' meaning that reference is modified but inputText is not. And I don't understand why...

Because you're modifying "reference" and not "inputText." Y9u have answered your own question. Now try dereferencing the reference and see what you get.

##### Share on other sites
Quote:
Original post by Bregma
Quote:
 Original post by MerlinoClearly, what does not work is reference = '\0' meaning that reference is modified but inputText is not. And I don't understand why...

Because you're modifying "reference" and not "inputText." Y9u have answered your own question. Now try dereferencing the reference and see what you get.

You'll get a memory protection error.

##### Share on other sites
You have modified reference, not the string pointed to by reference and inputText. What I think you meant to do was:

*reference = '\0'

reference = '\0' will set the pointer named reference to NULL. *reference = '\0' will set the value of the character the pointer named reference points to to the end-of-text marker.

##### Share on other sites
Quote:
 Original post by Ozymandias42You have modified reference, not the string pointed to by reference and inputText. What I think you meant to do was:*reference = '\0'reference = '\0' will set the pointer named reference to NULL. *reference = '\0' will set the value of the character the pointer named reference points to to the end-of-text marker.

That anonymous post above yours was mine, but I forget to log in. This is a char* string literal and it is not allowed to be modified through this pointer.

// char* inputText = "BlaBla XXBlaBla";// should bechar inputText[] = "BlaBla XXBlaBla";// and reference = '\0'// should be*reference = '\0';

##### Share on other sites
Quote:
 Original post by AbsolutionThat anonymous post above yours was mine, but I forget to log in. This is a char* string literal and it is not allowed to be modified through this pointer.

Maybe, maybe not. Depends on the compiler, the platform, etc. Certainly quite likely, though. I was assuming it was example code, since if he were actually using a constant, there'd be no resaon to hunt down the XXs at runtime.

##### Share on other sites
Oh I see what you're trying to do!
   char imputText [] = "BlaBla XXBlaBla";   reference[0]='\0';   font->RenderFont(0, 0, font->m_fontListBase, imputText);    font->RenderFont(0, 30, font->m_fontListBase, &reference[2]);

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 17
• 14
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
634094
• Total Posts
3015500
×