Monday, August 30, 2010

Resetting a string

My prygram goes through a string to find names between '\'-characters
The problem is, parts of the name in sTemp remains if the new name is
shorter than the old name.

char sTemp[50];
char sText[] = "THELONGESTNAME\\samantha\\gregor\\spike\\..." ; // and
so on...

some code

int i=0;
while((sText[i] != '\\'))
sTemp[i] = sText[i];
printf("Name: %s\n",sNameBuff);
// Here i want to reset the string, totally empty it.
// this dosent do it.
sNameBuff == "";
// Start over again...

You don't show its definition or usage, but assuming
that 'sNameBuff' is a pointer to your string (or the
name of an array containing it), write:

*sNameBuff = 0;


sNameBuff[0] = 0;

There may indeed still exist characters in subsequent
memory locations, but because the first character
is now zero, subsequent characters are no longer part
of your string, it's "empty".

No comments:

Post a Comment