I was wondering, once an input has been stored in a temp array. How do I "flush" out the datas in the array so that it can store another input for the second time.
I was wondering, once an input has been stored in a temp array. How do I "flush" out the datas in the array so that it can store another input for the second time.
If you're just going to overwrite the current data, there's no point in clearing it. If you want to do this for debugging purposes, just fill the array with a zero value. By 'zero value' I mean something predictable but appropriate for the type and use of the array. It doesn't actually have to be 0.
Just because I don't care doesn't mean I don't understand.
Hmm..well I'm trying to to move a string from one place to another. Since the total length of the string is different. I won't be able to overwrite the temporary data completely.
Just terminate your string with a null character correctly, and you don't have to worry about overwriting it. However, if you still want to, consider looking at the memset function.
Quzah.
Hope is the first step on the road to disappointment.
Code:for (i = 0; i != '\0' ; i++){ string[i] = ' '; }
well --- your example will do nothing at all. Why? the loop starts out by setting i to be 0, then it wants to continue the loop as long as i is not 0, but since i is initialized to 0 the loop will do nothing. maybe something like this is intended. Note that it is faster and more efficient than using the loop approach. This approach will only work when string is a char*. To set arrays of other data types, such as int, to something other than 0 you need the loop.Originally Posted by ReXXuSS
Code:memset(string,' ',strlen(string));
Last edited by Ancient Dragon; 09-06-2005 at 09:19 PM.
Both are incorrect however, because by "clearing" a string, you wouldn't set it to all spaces. If you did, and you were doing as the OP states by just copying the new string (minus the null) over top, it would still run the full length of the allocated space, because you've got it full of spaces. That is to say, if the string was 10 characters, and you space filled it, then copied "bob" to it, every time you wanted to print the string, or what not, you'd get:I somehow don't think that's what they had in mind.Code:"bob "
Quzah.
Hope is the first step on the road to disappointment.
Its true about what he said, I was thinking that if I use memset as how Ancient Dragon suggested it and I do a 'strlen', I would get a very large empty string. That would probably solve my overwritting problem.Originally Posted by quzah
However, it might give me a problem further in the program. I was thinking that I could somehow reset to '\0' .
As I've just learnt about the memset function. I was thinking if this would work.
Code:memset(string,'\0',strlen(string));
I think it should be:
Code:memset(string,'\0',strlen(string)-1);
Ask smart questions - if I would be that smart, I wouldn't need to ask questions.
That's an interesting thought, but the original was correct. Try it out by "clearing" the string with spaces.Originally Posted by Ene Uran
@OP: Why is simply setting the first character of the string to be cleared to 0 insufficient?[edit]Nevermind, it made more sense after reading it the second time.Code:int main() { char a[] = "awesome"; printf("%s\n",a); a[0] = 0; printf("%s\n",a); return 0; }
If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein
if string is not a pointer then use sizeof operatorOriginally Posted by DarrenY
Code:char string[80]; memset(string,0,sizeof(string));
Or as stated in my first reply, why not just copy the null character over when you copy in your new string?Originally Posted by pianorain
Quzah.
Hope is the first step on the road to disappointment.
And use strcpy() wil do the trick, assuming destination string is big enough to hold all the characters of the source string. I sometimes clear the destination buffer anyway -- for debugging purposes. It makes it easier to find buffer overflows. Other than that, its a waste of good cpu cycles to clear a buffer then turn right around and copy another string into it.Originally Posted by quzah