while (array[i] != "") {
array[i] = "";
}
But that doesn't really seem right for some reason. I though of using { }, but I'm not sure how that would work, exactly. Any ideas?
Best way to wipe an array?
Started by Uninverted, Mar 03 2008 02:59 PM
7 replies to this topic
#1
Posted 03 March 2008 - 02:59 PM
What's the 'best' way to make all of the elements in an array null (in terms of efficiency and simplicity)? The first thing I thought of was a loop, like, say,
My other car return the first item in a list.
|
|
|
#2
Posted 03 March 2008 - 03:11 PM
Well it sort of depends on the type of the array. If your using chars or ptrs then something like this should be efficient:
int i=0;
while(array[i] != NULL){
array[i] = NULL;
i++;
}
-Dustin
www.theCprogrammer.com
www.theCprogrammer.com
#3
Posted 03 March 2008 - 04:30 PM
Just call ZeroMemory(pointer,size)
#4
Posted 03 March 2008 - 09:55 PM
Watch out with the ZeroMemory-function, because it's a Win32 API function. It's probably a good alternative, if you're on Windows, but won't work on other platforms.
I would use the standard-C function, memset. I don't know about how efficient it is, though.
I would use the standard-C function, memset. I don't know about how efficient it is, though.
#include <string.h> // ... memset(my_array, '\0', sizeof(my_array[0]) / sizeof(my_array));
#5
Posted 04 March 2008 - 03:59 AM
memset is very efficient.
#6
Posted 04 March 2008 - 05:46 PM
v0id: your code won't work - the last parameter should be sizeof(my_array) / sizeof(my_array[0]). Sorry, I'm anal-retentive.
#7
Posted 04 March 2008 - 09:56 PM
Yes.
#8
Posted 05 March 2008 - 05:11 AM
I dynamically create them and then use free();
Programming Assignment Help
while(true) { cout << "Idiot!" << endl; }
while(true) { cout << "Idiot!" << endl; }


Sign In
Create Account


Back to top









