The first declaration:
char buf[10] = "";is equivalent to
char buf[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};The second declaration:
char buf[10] = " ";is equivalent to
char buf[10] = {' ', 0, 0, 0, 0, 0, 0, 0, 0, 0};The third declaration:
char buf[10] = "a";is equivalent to
char buf[10] = {'a', 0, 0, 0, 0, 0, 0, 0, 0, 0};
As you can see, no random content: if there are fewer initializers, the remaining of the array is initialized with 0. This the case even if the array is declared inside a function.
The first declaration:
char buf[10] = "";is equivalent to
char buf[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};The second declaration:
char buf[10] = " ";is equivalent to
char buf[10] = {' ', 0, 0, 0, 0, 0, 0, 0, 0, 0};The third declaration:
char buf[10] = "a";is equivalent to
char buf[10] = {'a', 0, 0, 0, 0, 0, 0, 0, 0, 0};
As you can see, no random content: if there are fewer initializers, the remaining of the array is initialized with 0. This the case even if the array is declared inside a function.
These are equivalent
char buf[10] = ""; char buf[10] = {0}; char buf[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};These are equivalent
char buf[10] = " "; char buf[10] = {' '}; char buf[10] = {' ', 0, 0, 0, 0, 0, 0, 0, 0, 0};These are equivalent
char buf[10] = "a"; char buf[10] = {'a'}; char buf[10] = {'a', 0, 0, 0, 0, 0, 0, 0, 0, 0};
How come this works:
Char name[20] = "Mary";
But this doesn't.
Char name[20];
Name[20] = "Mary";
Am I correct in that the above fails because I'm telling it to put "Mary" in the name[20] element? Is that how it's interpreted?
Is there a way to initialize char name[20] array separately from the declaration without using srtcpy()? Just wondering
Thanks
In addition to Will Dean's version, the following are common for whole buffer initialization:
Copychar s[10] = {'\0'};
or
Copychar s[10];
memset(s, '\0', sizeof(s));
or
Copychar s[10];
strncpy(s, "", sizeof(s));
You want to set the first character of the string to zero, like this:
Copychar myString[10];
myString[0] = '\0';
(Or myString[0] = 0;)
Or, actually, on initialisation, you can do:
Copychar myString[10] = "";
But that's not a general way to set a string to zero length once it's been defined.
It's anyways bad practice to initialie a char array with a string literal.
The author of that comment never really justifies it, and I find the statement puzzling.
In C (and you've tagged this as C), that's pretty much the only way to initialize an array of char with a string value (initialization is different from assignment). You can write either
char string[] = "october";
or
char string[8] = "october";
or
char string[MAX_MONTH_LENGTH] = "october";
In the first case, the size of the array is taken from the size of the initializer. String literals are stored as arrays of char with a terminating 0 byte, so the size of the array is 8 ('o', 'c', 't', 'o', 'b', 'e', 'r', 0). In the second two cases, the size of the array is specified as part of the declaration (8 and MAX_MONTH_LENGTH, whatever that happens to be).
What you cannot do is write something like
char string[];
string = "october";
or
char string[8];
string = "october";
etc. In the first case, the declaration of string is incomplete because no array size has been specified and there's no initializer to take the size from. In both cases, the = won't work because a) an array expression such as string may not be the target of an assignment and b) the = operator isn't defined to copy the contents of one array to another anyway.
By that same token, you can't write
char string[] = foo;
where foo is another array of char. This form of initialization will only work with string literals.
EDIT
I should amend this to say that you can also initialize arrays to hold a string with an array-style initializer, like
char string[] = {'o', 'c', 't', 'o', 'b', 'e', 'r', 0};
or
char string[] = {111, 99, 116, 111, 98, 101, 114, 0}; // assumes ASCII
but it's easier on the eyes to use string literals.
EDIT2
In order to assign the contents of an array outside of a declaration, you would need to use either strcpy/strncpy (for 0-terminated strings) or memcpy (for any other type of array):
if (sizeof string > strlen("october"))
strcpy(string, "october");
or
strncpy(string, "october", sizeof string); // only copies as many characters as will
// fit in the target buffer; 0 terminator
// may not be copied, but the buffer is
// uselessly completely zeroed if the
// string is shorter!
The only problem I recall is assigning string literal to char *:
char var1[] = "september";
var1[0] = 'S'; // Ok - 10 element char array allocated on stack
char const *var2 = "september";
var2[0] = 'S'; // Compile time error - pointer to constant string
char *var3 = "september";
var3[0] = 'S'; // Modifying some memory - which may result in modifying... something or crash
For example take this program:
#include <stdio.h>
int main() {
char *var1 = "september";
char *var2 = "september";
var1[0] = 'S';
printf("%s\n", var2);
}
This on my platform (Linux) crashes as it tries to write to page marked as read-only. On other platforms it might print 'September' etc.
That said - initialization by literal makes the specific amount of reservation so this won't work:
char buf[] = "May";
strncpy(buf, "September", sizeof(buf)); // Result "Sep"
But this will
char buf[32] = "May";
strncpy(buf, "September", sizeof(buf));
As last remark - I wouldn't use strcpy at all:
char buf[8];
strcpy(buf, "very long string very long string"); // Oops. We overwrite some random memory
While some compilers can change it into safe call strncpy is much safer:
char buf[1024];
strncpy(buf, something_else, sizeof(buf)); // Copies at most sizeof(buf) chars so there is no possibility of buffer overrun. Please note that sizeof(buf) works for arrays but NOT pointers.
buf[sizeof(buf) - 1] = '\0';
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).
EDIT In order to use memset, you have to include string.h.
Depends on what you mean by 'empty':
members[0] = '\0';