Empty an array of characters.

if I have an array of characters. How can I empty this array of characters?

I need to empty the array of characters in order to re-use it in the next tokens.



My code:




char id[30];
char lName[30];
char fName[30];
char status[30];


getline(i, sentence);

cString = new char[sentence.length() + 1];
strcpy(cString, sentence.c_str());
tokenPtr = strtok(cString, ",");
strcpy(id, tokenPtr);

id[0] = '/0'; <-------- code does not empty the array or gives errors
- What is the data type for "cString"?

- Can you post more of your code? Depending on if you are using functions or not we may be able to deallocate "id" at the end of the scope so that when the function is called again it get's redeclared.

- IMO the variable "id" would be better off as a char pointer (char*) instead of a char array, then you could just set "id" to the value returned by the "strtok()" function and then in order to clear it you set "id" back to NULL.
Last edited on
You could iterate through every element of the array and set it to the \0 null value like this:
1
2
3
for(size_t i = 0; i < length; i++) {
    str[i] = '\0';
}
Topic archived. No new replies allowed.