Jump to content
  • Advertisement
Sign in to follow this  
Merlino

Char* string: How To??

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Merlino
Clearly, 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 this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Bregma
Quote:
Original post by Merlino
Clearly, 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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Ozymandias42
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.


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 be

char inputText[] = "BlaBla XXBlaBla";

// and reference = '\0'
// should be

*reference = '\0';


Share this post


Link to post
Share on other sites
Quote:
Original post by Absolution
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.


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 this post


Link to post
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]);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!