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"
Depends on what you mean by 'empty':
members[0] = '\0';
Use bzero(array name, no.of bytes to be cleared);
You can use the following instruction:
strcpy_s(members, "");
I had similar problem. I was trying to display results of Analog to Digital value from a Char type Array. The problem was when I was turning the Pot to get lower voltage or lower converted decimal value like 5 from between (1023 - 0), the lift over characters from array was staying beside the number 5. I used this method to get rid of the problem:
LCD_Send_String(" "); \ used spaces as string characters LCD_Send_Command (LCD_THIRD_ROW); \Returned the cursor back at the start of line.
I'd go with
members_in_use = 0;
EDIT: Given the most recent edit to the question, this will no longer work as there is no null termination - if you tried to print the array, you would get your characters followed by a number of non-human-readable characters. However, I'm leaving this answer here as community wiki for posterity.
char members[255] = { 0 };
That should work. According to the C Programming Language:
If the array has fixed size, the number of initializers may not exceed the number of members of the array; if there are fewer, the remaining members are initialized with 0.
This means that every element of the array will have a value of 0. I'm not sure if that is what you would consider "empty" or not, since 0 is a valid value for a char
.
simpler is better - make sense?
in this case just members[0] = 0 works. don't make a simple question so complicated.
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, "hello");
Concatenate (add the string):
strcat(members, " world");
Empty string:
members[0] = 0;
Simple like that.
char members[255] = {0};
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.
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");
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);
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..
Source: Stackoverflow.com