how do i make a char array null after filled with values

Started by
8 comments, last by Nibbles 22 years ago
Hi, I''ve filled a char array, char string[80], with values and such, and now i need to clear it all (make it null), so i can add different values. I was wondering how I would do this. Thanks, Scott ------------------------------------------------------------ Email Website
"If you try and don''t succeed, destroy all evidence that you tried." ------------------------------------------------------------
Advertisement

memset(char_array, 0, char_array_length); 
try using memset(void *dest, int c, size_t count)

such as
memset(string, 0, 80), this set all the entries in the array string to 0....I hope this helps.


[edited by - Dr Boo on March 22, 2002 3:41:10 AM]

[edited by - Dr Boo on March 22, 2002 3:41:55 AM]
As mentioned, you can use the standard library function:

void *memset(void *dest, int byte_value, size_t length);
(located in string.h)

Why do you need to clear it so you can add different values? It seems to me it would be more effective to just let the code that sets the values also terminate the string. (I know this isn''t as "safe" as clearing the whole buffer everytime you''re going to write something to it.)

Cheers mate

Q: Why do programmers always get Christmas and Halloween mixed up?
A: Because DEC 25 = OCT 31
Q: Why do programmers always get Christmas and Halloween mixed up?A: Because DEC 25 = OCT 31
whoop, my bad. I figured out the problem right after I posted the question then I was having dns errors trying to get back into gamedev to delete it. the string I was working with actually contained the name of a .bmp file, and I thought that it wasn''t properly getting cleared, thus not having the right filename in the string, but it turned out that the .bmp file itself was corrupt which was causing my program to crash.

glad to know how to do it now though,
Thanks for all the help!

Scott


------------------------------------------------------------
Email
Website

"If you try and don''t succeed, destroy all evidence that you tried."
------------------------------------------------------------
ZeroMemory calls memset with 0, so it might be more readible.

char* vector = new char[vectorLen];
ZeroMemory(vector, vectorLen);
Since you''re talking strings strset might be a better choice than memset. It depends on exactly what you want to do.

-Mike

-Mike
or just to be cool, you can cook up your own ZeroMemory:


  inline void ZeroMemory(void *target, size_t size){    memset(target,0,size);}  
quote:Original post by Muusu
As mentioned, you can use the standard library function:
Q: Why do programmers always get Christmas and Halloween mixed up?
A: Because DEC 25 = OCT 31


That is really funny

You could use memset, or you could also do this too:

  for (int i = 0; i <= size; i++){    dasArray[i] = NULL;}  






_____________________________________________________

ICQ #: 149510932

Google - OpenGL - DirectX - Windows Guide Network - MSDN - Symantec Virus Info

"Imagination is more important than knowledge." - Albert Einstein

A C string is terminated at the first null; why bother clearing the whole thing?


  str [0] = ''\0'';//Or...*str = ''\0'';  

This topic is closed to new replies.

Advertisement