Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clear a String

Status
Not open for further replies.

selenge

Programmer
Sep 5, 2002
1
AU
How do I clear a string....?

strcat(temp, file);
How do I set temp to all null values after a first round of strcat? my strcat is in a loop....
 
When working with strxxx() functions, it will normally be enough to clear only the first byte in the array :

temp[0] = 0;

If you want to clear the whole string :

memset(temp, 0, size_of_temp);


/JOlesen
 
Warning about JOlesen's solution: size_of_temp depends on the declaration of temp. If temp is declared as an array, it will work

char temp[MAX];
memset (temp, 0, sizeof (temp));

A common mistake is if it is dynamically allocated. You have to use the size allocated otherwise it will just nullify sizeof(ptr) which, on my compiler is 4.

char* ptr;
ptr = (char*) malloc (MAX);
memset (temp, 0, MAX);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top