Archived

This topic is now archived and is closed to further replies.

ChaosPhoenix

Arg! Getting a NULL return and don't know why.

Recommended Posts

void CheckForCommand(char *szText)
{
	char* buffer1;
	char* Temp;
	Temp = szText;
	buffer1 = strtok(Temp," ");
	if(!stricmp(buffer1,"?"))
	{
		DisplayText(hLB_Output,"List of Commands:");
		DisplayText(hLB_Output,"? = Help");
		DisplayText(hLB_Output,"/nick  to change your Nickname");
		DisplayText(hLB_Output,"/server to display the current Server version");
	}
	if(!stricmp(buffer1,"/nick"))
	{
		ChangeNick(szText);
	}
}

void ChangeNick(char *szText)
{
	
	char* buff1;
	char* buff2;
	
	buff1 = szText;
	buff2 = strtok(buff1," ");
	buff2 = strtok(NULL," ");
	
	if(buff2 == NULL)
	{
		DisplayText(hLB_Output,"NULL returned");
		return;
	}
	DisplayText(hLB_Names,buff2);
	DisplayText(hLB_Output,"Name Changed.");
}
 
Okay, so this is my Personal IRC client/server I''ve been working on and for some reason I keep getting a NULL return when I try and split the string to use the "/nick" command. I''ve tried making copies of the original variable and have no idea why Im still getting a NULL returned. My synax appears to be right for strtok and I''ve never had problems with using it before but this time I keep getting that stupid NULL value. Anyone see my error? P.S. Yes, I will be using case/switch in the final version, the if/multiple functions is just for debugging purposes.

Share this post


Link to post
Share on other sites
quote:
Original post by ChaosPhoenix
I''ve tried making copies of the original variable and have no idea why Im still getting a NULL returned.

Not in the code above.

quote:

My synax appears to be right for strtok and I''ve never had problems with using it before but this time I keep getting that stupid NULL value.

Observe:
// in CheckForCommand:
Temp = szText;
buffer1 = strtok(Temp," ");

// in ChangeNick:
buff1 = szText;
buff2 = strtok(buff1," ");
buff2 = strtok(NULL," ");

Condensing this into a single execution path, you obtain:

buffer1 = strtok(szText," ");
buff2 = strtok(szText," "); // uh-oh...
buff2 = strtok(NULL," ");


strtok (destructively?) uses an internal buffer to tokenize, which is why the string argument after the first should be NULL to continue obtaining tokens from the same expression. Condensed as above, it becomes apparent that is not the case with your code (pointer assignment doesn''t make a copy of the data pointed to).

In short, if you were trying to preserve your string argument (szText), then allocate memory and make a real copy before tokenizing. This is an example of where const-correctness (in C++) can point out non-obvious logic errors.

Share this post


Link to post
Share on other sites
Yes, I love that little function. Just remember to make a clone of the string you are tokenising. I always like this:

my strtok utilizing function:

....


  
void (char *String)
{
char *buffer,*tok;
buffer = new char[strlen(String)+10];

strcpy(buffer, String);

// do your strtoking with buffer like so

tok = strtok(buffer, " ");

...

tok = strtok(NULL, " ");

...

// then if you want to ever reference the exact location in

// the unmodified String do use the following:


// for the character at your current location

String[tok-buffer];

// for the pointer to the string at the current location

String + (tok-buffer);
//or

&String[tok-buffer];

}



....

Always leaves you with a lot more control over your data... and leaves an unmodified copy for you to check back on when you don''t want to destroy all the instances of that string...

-Chris Bennett of Dwarfsoft - The future of RPGs GPA Thanks to all the goblins in the GDCorner niche

Share this post


Link to post
Share on other sites