Question:
How do you empty an array in C++?
2006-10-05 18:15:24 UTC
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?
Seven answers:
Digitally Й!Й
2006-10-05 21:09:43 UTC
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;



str[0] = '\0';
barrabe
2006-10-06 19:22:50 UTC
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 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 << str << endl; // output with c++ style iostreams

str.clear(); // clear the string.
Jay
2006-10-06 02:26:46 UTC
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.
kinghezzy
2006-10-06 02:19:01 UTC
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



int main() {



char str[81];



str[0] = 'a';

str[1] = 0;



std::cout << 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.
naga
2006-10-06 02:05:17 UTC
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).
Fabo
2006-10-06 01:18:00 UTC
str = 0;
2006-10-06 06:47:12 UTC
use ReDim


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...