tags:

views:

873

answers:

12

Have an array of chars like char members[255]. How can I empty it completely without using a loop?

char members[255];

By "empty" I mean that if it had some values stored in it then it should not. For example if I do strcat then old value should not remain

members = "old value";

//empty it efficiently
strcat(members,"new"); // should return only new and not "old value new"
+4  A: 

I'd go with

members_in_use = 0;
pmg
-1 that doesn't make any sense.
Kinopiko
It actually makes a lot of sense, since the OP is asking for an operation (emptying) that is not part of a C array - therefore, ideally, he'd implement a new data structure, and pmg's way of signaling the structure is empty is good enough.
Michael Foukarakis
No, it really doesn't make any sense.
Kinopiko
Kinopiko: Apparently you don't understand the approach then
Christian
Kinopiko: read starblue answer as it explains my reasoning better than I can explain in a comment (thank you starblue)
pmg
+2  A: 

char members[255] = {0};

-1 That is an initializer, it doesn't empty the array.
Kinopiko
and you give me minus after he edited his question
This answer was wrong before he edited the question.
Kinopiko
Nope, because he didn't specify that it must be done after initializing. Especially this may have been understood by his first example before edit and because it's also very common use case to clear an array.
+15  A: 

using

  memset(members, 0, 255);

in general

  memset(members, 0, sizeof members);

if the array is in scope, or

  memset(members, 0, nMembers * (sizeof members[0]) );

if you only have the pointer value, and nMembers is the number of elements in the array.


EDIT Of course, now the requirement has changed from the generic task of clearing an array to purely resetting a string, memset is overkill and just zeroing the first element suffices (as noted in other answers).

Steve Gilham
+1 - I would go for this as it ensures that all array elements are in a known state.
ChrisBD
It goes against the (epic) requirement of not using a loop, though.
Michael Foukarakis
While memset is probably not implemented as a recursion, it could be -- in either case, the iteration is decently hidden from sight, which was the ostensible requirement.
Steve Gilham
+14  A: 

Depends on what you mean by 'empty':

members[0] = '\0';
Felix
That would only change the value of members[0], not members[1] through members[254]. Fine if you are accessing sequentially...but not so fine in other cases.
Thomas Owens
After the OP's edit it seems my solution should work for his requirements, as strcat() will find the NULL byte and treat it accordingly.
Felix
nevertheless memset seems to be a better solution
Alex Xander
Not a better solution. Just a solution to a different problem.
ypnos
Alex Xander: memset is not the better solution, at least not in your case. As pointed by others, memset() will actually loop through the array, even if you don't actually see a "for" statement.
Felix
There is no need to use memset() when every known version of strcat() will handle NULL in the same way. In a similar, but simpler logic, why shred README files when they can just be deleted?
Tim Post
There's no way to "empty" an abstract array in C. Every array constains something, even if it is zeroes. The notion of "emptying an array" is only applicable to higher-level logical interpretations of array contents. For example, the contents of the array might be interpreted as a C-string. In that case the proper way to "empty" it is to set the very first element to '\0'. Exactly what Felix did. I don't know what other interpretations the OP might have in mind, but without making it more specific all those attempts with 'memset' are just meaningless answers to a meaningless question.
AndreyT
@tinkertim: There's no NULL here. And no, 'strcat' does not know how to handle NULL. It will crash if you pass NULL to it.
AndreyT
+2  A: 
Thomas Owens
-1 that is an initializer.
Kinopiko
It depends on how you define "empty". The char with a value of 0 is not a human-readable character and the array of chars is in a known state. I would consider that empty. It also does the exact same thing as memset (if my recollection of memset is correct - it's been too long since I've used C).
Thomas Owens
A: 

By "empty an array" if you mean reset to 0, then you can use bzero.

#include <strings.h>  
void bzero(void *s, size_t n);

If you want to fill the array with some other default character then you may use memset function.

#include <string.h>  
void *memset(void *s, int c, size_t n);
secureBadshah
bzero and memset (usually) both use a loop.
Michael Foukarakis
+3  A: 

You cannot empty an array as such, it always contains the same amount of data.

In a bigger context the data in the array may represent an empty list of items, but that has to be defined in addition to the array. The most common ways to do this is to keep a count of valid items (see the answer by pmg) or for strings to terminate them with a zero character (the answer by Felix). There are also more complicated ways, for example a ring buffer uses two indices for the positions where data is added and removed.

starblue
+1  A: 
members[0] = 0;

is enough, given your requirements.

Notice however this is not "emptying" the buffer. The memory is still allocated, valid character values may still exist in it, and so forth..

Michael Foukarakis
Can be also written as *members = 0;
eyalm
A: 

Disclaimer: I don't usually program in C so there may be any syntax gotcha in my examples, but I hope the ideas I try to express are clear.

If "emptying" means "containing an empty string", you can just assign the first array item to zero, which will effectively make the array to contain an empry string:

members[0] = 0;

If "emptying" means "freeing the memory it is using", you should not use a fixed char array in the first place. Rather, you should define a pointer to char, and then do malloc / free (or string assignment) as appropriate.

An example using only static strings:

char* emptyString="";
char* members;

//Set string value
members = "old value";

//Empty string value
member = emptyString

//Will return just "new"
strcat(members,"new");
Konamiman
A: 

simpler is better - make sense?

in this case just members[0] = 0 works. don't make a simple question so complicated.

EffoStaff Effo
+2  A: 

Given the bickering that is going on for such a basic operation, I thought I'd post something less silly than that.

The solution is simple, try this:

/* Dear Compiler:
 * Please magically set the first byte to NULL when I need it
 * to be :) Thank you!
 */
Tim Post
Rofl.. you made me cryin'.
Andrejs Cainikovs
#pragma artificial_intelligence
ammoQ
i tried "dear compiler". it works!!
EffoStaff Effo
How can you set a byte to NULL? What's 'NULL' doing in this context?
AndreyT
well, NULL: Nothing yoU'Ll Learn...
EffoStaff Effo
AndreyT, don't ruin this awesome post. FYI, NULL==0.
Andrejs Cainikovs
@AndreyT FFS ... By setting a byte to NULL you (typically) set it to whatever the platform favors as an escaped '0'. Since some early platforms argued as to what that should be, the NULL macro emerged as a favorite in the very first C standard. If I have array[100], I am free to set array[x] to what is globally understood as an empty but initialized value. So to set a byte to NULL, I simply : array[11] = NULL; Whereby I've told the platform (or any portable function reading / itering the array) to STOP. Please elaborate on what you mean by 'context' :)
Tim Post
@AndreyT Or even , for your pleasure, CAST IT! your_argument = (char *) NULL;
Tim Post
s/NULL/nul/g and all is well
profjim
+1  A: 

Don't bother trying to zero-out your char array if you are dealing with strings. Below is a simple way to work with the char strings.

Copy (assign new string):

strcpy(members, "blond girls");

Concatenate (add the string):

strcat(members, " are stupid!");

Empty string:

members[0] = 0;

Simple like that.

Andrejs Cainikovs
i agree, but strcat is very expensive, strcpy(members, "blond girls"), strcpy(members + N, " are stupid!"), will be better.
EffoStaff Effo
Sure, this is only for a reference.
Andrejs Cainikovs