Jump to content
  • Advertisement
Sign in to follow this  
pbbm2000

char help

This topic is 5398 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

class cData { public: char *dat; int length; cData() { dat = new char[50]; length = -1; } }; void Rot() { cData text[17]; text[1].dat[0] = text[0].dat[0]; text[0].dat[0] = NULL; } if I do that then text[1].dat[0] is also set to NULL. How can I set text[0].dat[0] to NULL and have text[1].dat[0] keep its data? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by pbbm2000
if I do that then text[1].dat[0] is also set to NULL.

No it isn't. It might already be NULL, but it won't be changed by that last line.
Quote:
How can I set text[0].dat[0] to NULL and have text[1].dat[0] keep its data?

Just like that. However, the line "text[1].dat[0] = text[0].dat[0];" probably doesn't do what you think it does. It copies over the first element of text[1].dat into the first element of text[0].dat. The rest of the char array is not copied. And since neither array is initialized, the results are undefined (read: a bug).

Share this post


Link to post
Share on other sites
I inilitlize all the elements to null like this.

for(int a = 0; a < 17; a++)
{
for(int b = 0; b < max; b++)
{
text[a].dat = NULL;
}
}

I have some text in text[0].dat and I want to move it to text[1].dat then get rid of all the text in text[0].dat.

for(int a = 0; a < max; a++)
{
text[1].dat[a] = text[0].dat[a]e;
text[0].dat[a] = NULL;
}

after I do this text[1].dat[x] will equl null. Also if I set text[0].dat[x] = 'j' (or any charter) then text[1].dat[x] will also equal that char. Am I somehow making text[1].dat point to text[0].dat?

Share this post


Link to post
Share on other sites
Uh... the *elements* of dat are type char. You can't set them to "NULL" in the sense that that is normally understood. You can certainly set them to value 0 (which is what NULL amounts to in C++, no void* nonsense), which is ascii NUL.

Other than that... try making sure each element of text is a different cData.

Share this post


Link to post
Share on other sites
Do not mix NULL with chars, use only '\0' for a char, and only NULL for a pointer to a char, and 0 for an integer etc.
Otherwise you will confuse yourself and everyone else.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!