Tuesday, July 14, 2009

How do you empty an array in C++?

I need to empty an array under a certain condition. When I declare the array, I declare it as:





char str[81];





I fill it up by reading in characters from a file, and, when I move to the next line in the file, I want to empty out all of those characters to fill it up again, with new characters.





Will adding a '\0' after the last character in the second set of characters have the same effect as re-initializing the array? Is there a better way?

How do you empty an array in C++?
I agree with "Jay".


u wont need to empty it. it'll just get overwritted when u read new line from the file. Adding NULL after the last character...denotes that the string should be read upto NULL.


So every thing u get a line read in the str, add a NULL at the end.





if u want to empty a string u can do this ..





str[0] = NULL;


%26lt;or%26gt;


str[0] = '\0';
Reply:Adding a '\0' after the last character has been placed in the array is a good idea since 99.9% C and C++ functions expect character array strings to be null terminated (otherwise they would not be able to know where the end of the string is?). But that's not reinitializing the array. There is no "reinitializing" needed. once you're done with that line, just start again at index zero and read the next line like you read the last one. A character array as declared is nothing more than a small block of memory.
Reply:use ReDim
Reply:the previous answer:


str = 0;


is a really bad idea, and the compiler shouldn't let you do it...





To answer your question, most functions in C++ will accept an array of characters in which there are some number of characters followed by a null character and treat that as a string.





So, for instance,





#include %26lt;iostream%26gt;





int main() {





char str[81];





str[0] = 'a';


str[1] = 0;





std::cout %26lt;%26lt; str;





return 0;


}





will print out the letter 'a'.





Assuming you are passing your array of characters to something that works this way (looks for a series of characters followed by a null character), the scheme you've decribed (setting "the last character in the second set of characters" to 0) should work for you.
Reply:str = 0;
Reply:When you declare char str[81] what you are really doing is allocating a block of 81 contiguous (sequential) characters, then setting str to point to that block. So, it's generally a bad idea to later assign str to NULL or 0.





As others have mentioned, you don't really need to reinitialize, just reuse the same array and it'll overwrite. However, if you're using the "array" as a string you'll want to add a '\0' to the end after reading each line, so it'll print out and work with string functions fine.





If you really want to initialize the array to some value, the fastest way is with memset(): memset(str, 0, 81); This will fill the memory block with zeroes (the second argument).





BTW, if you're using C++ you probably want to learn about the STL (standard template library) and use either a vector%26lt;char%26gt; or string object. It's much simpler and safer than using C-style arrays. For example:





string str = "";


str += 'a'; // append character 'a' to the end of the string


printf("%s\n", str.c_str()); // output with printf()


cout %26lt;%26lt; str %26lt;%26lt; endl; // output with c++ style iostreams


str.clear(); // clear the string.
Reply:one thing I need to suggest u is that there is no need to empty an array.


just initialize to some value like 'NULL' (zero is ascii value of null). It helps to verify whether an element is empty or not(in this case null or not).


No comments:

Post a Comment