Archived

This topic is now archived and is closed to further replies.

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

This topic is 5784 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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." ------------------------------------------------------------

Share on other sites

memset(char_array, 0, char_array_length);

Share on other sites
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]

Share on other sites
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

Share on other sites
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."
------------------------------------------------------------

Share on other sites
ZeroMemory calls memset with 0, so it might be more readible.

char* vector = new char[vectorLen];
ZeroMemory(vector, vectorLen);

Share on other sites
Since you''re talking strings strset might be a better choice than memset. It depends on exactly what you want to do.

-Mike

Share on other sites
or just to be cool, you can cook up your own ZeroMemory:

  inline void ZeroMemory(void *target, size_t size){    memset(target,0,size);}

Share on other sites
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

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

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