Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to re-initialize an array ??? 1

Status
Not open for further replies.

ab1966

Programmer
Jul 3, 2001
6
0
0
US
I have declared an array of characters as follows :

static char my_array[ARR_LEN];

After each iteration I want to clear the array. How do I do that ? Any help will be appreciated.
 
If you're using the array to hold strings, you can simply set the first element to the null terminator:

my_array[0]=0;

If you really want to destroy all data in the array, you can use memset():

memset(my_array,0,sizeof my_array);

This can get expensive though if ARR_LEN is a large quantity and/or you're iterating a lot. Probably the first method is what you want.
Russ
bobbitts@hotmail.com
 
well the simplest way of course is to just do this:

for(i = 0; i < ARR_LEN; i++)
my_array = '\0';
 
wtf?? my_array should of course have after it.. i typed it in above but apparently it was ignored??
 
memset(my_array, 0, sizeof(memset));

Note : if you are doing the program based on the '\0' then noneed to clear the array. The new string will store the '\0' in the array and it is problem freee.

Maniraja S
 
There is a trik with memset.
In reference said that you can set any value from -255 to 255 for each byte. So you can do it only for char array. For any non char array you can set only &quot;0&quot;, because if you put 0 in all 4 integer bytes you still have a zero and &quot;-1&quot;,
because it has all 1 in all bits and it will do the same thing with integer - put all 1 in all for bytes and your integer array will be initialize by &quot;-1&quot;.
Also you can use memset() for multidimensional arrays, for example:
int a [3][3][3];
memset (&a[0][0][0], 0, 27 * sizeof (int)); or
memset (&a[0][0][0], -1, 27 * sizeof (int));
 
Use memset()...it's good practice to clear the variable when it's not in use.
As for this...

for(i = 0; i < ARR_LEN; i++)
my_array = '\0';

What if it is a large array? This is added weight to the program, no need for this...definately stick with memset.

Nice tip, Lim ;)

Rob
&quot;Programming is like art...It makes me feel like chopping my ear off.&quot;
 
surprise! memset does exactly the same thing as the for loop
 
Hey,
I think memset is better coz u can use it for multi dimensional arrays too. imagine reinitialising using a loop for a multidimensional array. its goin to be hectic.
so go for memset.
Thanx
LOL
 
to cyberson: the memset and the loop are very closely like solutions. The actually difference, is that memset
usually is optimized with assembly, and/or loop unrolling techniques, but a simplistic, not ansi-correct, not bad-arg passing proof, implementation would be:

void memset(void *x, int c, int n) {
char *z = x;
while (n--) *z++ = c;
}

the idea to get is: to work with a multidimensional array, just cast the base pointer into a char * pointer, and run through the char * pointer for the total length of the multidimensional array.

cya

--
pkiller

 
i always use .....
Code:
my_array[0]=0;
// or
memset(&my_array,0,sizeof my_array);
....and thats it ...... more often
Code:
my_array[0]=0;
i never had problems with that. bluenote@uyuyuy.com
(excuse my english)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top