how to clear a char * variable in c++

Jay59express

Senior member
Jun 7, 2000
481
0
0
I have a char* variable, before, that is part of a loop, so i need to make its contents NULL after every pass of the loop. Before the next pass I tried to make a for loop that said before = '\0', for the length of the word, it compiles, but gives a runtime error, just saying before[0] = '\0', or = anything for that matter also gives a runtime error. Is there a function to clear out the char*
Thanks for any input!
 

PrincessGuard

Golden Member
Feb 5, 2001
1,435
0
0
How is before declared?

From what I can tell, you declared it like char *before. If so, then unless you do a before = new char[] somewhere, before will not point to a valid memory location for you to access.
 

Jay59express

Senior member
Jun 7, 2000
481
0
0
yes, it was declared as: char * before;
I can use it without saying before = new char[], it behaves just like a string. I just cant clear the damn thing
 

BFG10K

Lifer
Aug 14, 2000
22,709
3,000
126
*before = "";

That will make before point to a null terminated empty string, just make sure it points to a valid string array otherwise you'll overwrite memory and/or create memory leaks.
 

TheDiggler

Senior member
Dec 23, 2002
695
0
0
Whoooooooooooooooooooooa!

1) char *before; // This statement simply defines a pointer pointing to an unknown memory location.
2) char *before=NULL; // This statement defines a pointer pointing to NULL
3) char someStorage[50]; // This statement defines a static array of 50 bytes of char data
4) char *before=&someStorage[0]; // This statement defines a pointer to point to the starting location of someStorage
5) char *before=new char[50]; // This statement defines a pointer to point to 50 bytes of DYNAMICALLY ALLOCATED RAM.

If you're simply performing STEP #1 above and not pointing pointer-variable before to a DEFINED MEMORY LOCATION (be it a statically defined location or a dynamically defined location), you're clobbering over memory that's not yours to mess with. That's what's causing your runtime error.

Your logic for searching for the first '\0' is flawed in the sense that you can copy a smaller word into a larger memory location (i.e. copy the word "HELLO\0" into a 50 byte storage area). The data after the '\0' may not necessarily be NULL, and thus when you search for the first '\0', you'll miss the rest of a memory storage location. Thus, you should know the size of your data storage areas.

With the above said, please post more details (i.e. more of your code) so we can get a better understanding of what you're really trying to accomplish.
 

BFG10K

Lifer
Aug 14, 2000
22,709
3,000
126
Yeah, the safest way is to use a fixed sized string array:

char string [ARRAY_SIZE];
char *before = string;


then you can do *before=""; or even safer, strcpy (string, ""); to clear it.
 

Jay59express

Senior member
Jun 7, 2000
481
0
0
Well, I cannot really post code because i would basically have to post the whole thing. But here is a better description. It needs to stem the words (watching -> watch).
I am using the porter algorithm, and the only version i could find was a C version that accepts a char *. So i found an algorithm for converting strings to char * (return &input[0]). That works fine. The stem function takes the char*, the beginning of the word, 0, and the position of the last character, which I calculate when I am building the word, so that is no problem. It returns an integer which is the new last position of that char *. So I need to be able to copy the first n characters from the original char * into a fresh char *. After I am done with that word, I need to flush the char* so it wont contain anything from the previous word(s) which were longer than it.
 

Jay59express

Senior member
Jun 7, 2000
481
0
0
ahh,
I got it, after reading some of the posts carefully, i got it to work,thanks a bunch guys!
I had to just use a regular array, and make before point to the beginning of it as you mentioned, thanks!
 

TheDiggler

Senior member
Dec 23, 2002
695
0
0
Porter algorithm??? You programming or eating steak? :D
Ok, let's see if I digested what you said ...

I'm not exactly sure what your stem function's prototype is, so I'll try and guess it based on your description:

Guess #1:
void stem(char *before, char *wordStartPos, char *wordEndPos)
{
char localWord[100]; // Buffer to hold copy of word
int wordLen = (int)(wordEndPos - wordStartPos) + 1; // Calculate word length using pointer offset arithmetic

// Copy wordLen # of characters from before (pointed to by wordStartPos) into localWord
memcpy(localWord, wordStartPos, wordLen);

// NULL TERMINATE the new copy of the word
localWord[wordLen]='\0';

// Clear out the original word in before (pointed to by wordStartPos)
memset(wordStartPos, NULL, wordLen);
}

============================================================

Guess #2:
void stem(char *before, int wordStartPos, int wordEndPos)
{
char localWord[100]; // Buffer to hold copy of word
int wordLen = wordEndPos - wordStartPos + 1; // Calculate the word length

// Copy wordLen # of characters from before (at offset wordStartPos) into localWord
memcpy(localWord, before+wordStartPos, wordLen);

// NULL TERMINATE the new copy of the word
localWord[wordLen]='\0';

// Clear out the original word in before (again, at offset wordStartPos)
memset(before+wordStartPos, NULL, wordLen);
}

*EDIT* Obviously I was already working on this reply when you posted your reply that you figured out your problem. Maybe the above code will be useful to you anyway. <shrug>