Clearing elements in an array.

This is a discussion on Clearing elements in an array. within the C Programming forums, part of the General Programming Boards category; I was wondering, once an input has been stored in a temp array. How do I "flush" out the datas ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    54

    Clearing elements in an array.

    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.

  2. #2
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    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.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    54
    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.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    Code:
    for (i = 0; i != '\0' ; i++){
         string[i] = ' ';
    }

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by ReXXuSS
    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.
    Code:
    memset(string,' ',strlen(string));
    Last edited by Ancient Dragon; 09-06-2005 at 10:19 PM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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:
    Code:
    "bob       "
    I somehow don't think that's what they had in mind.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    54
    Quote Originally Posted by quzah
    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:
    Code:
    "bob       "
    I somehow don't think that's what they had in mind.


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

    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));

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    10

    Thumbs up

    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.

  10. #10
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by Ene Uran
    I think it should be:
    Code:
    memset(string,'\0',strlen(string)-1);
    That's an interesting thought, but the original was correct. Try it out by "clearing" the string with spaces.

    @OP: Why is simply setting the first character of the string to be cleared to 0 insufficient?
    Code:
    int main()
    {
    	char a[] = "awesome";
    	printf("%s\n",a);
    	a[0] = 0;
    	printf("%s\n",a);
    	return 0;
    }
    [edit]Nevermind, it made more sense after reading it the second time.
    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

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by DarrenY
    Code:
    memset(string,'\0',strlen(string));
    if string is not a pointer then use sizeof operator
    Code:
    char string[80];
    memset(string,0,sizeof(string));

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by pianorain
    Why is simply setting the first character of the string to be cleared to 0 insufficient?
    Or as stated in my first reply, why not just copy the null character over when you copy in your new string?


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by quzah
    Or as stated in my first reply, why not just copy the null character over when you copy in your new string?
    Quzah.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding More Array Elements?
    By Vermillion in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2008, 11:02 PM
  2. coping elements not in array
    By lord in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2008, 08:53 PM
  3. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. A simple question about selecting elements in an array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 11:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21