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
Videos
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';
Yes, it is the same. If there are less number of initializers than the elements in the array, then the remaining elements will be initialized as if the objects having static storage duration, (i.e., with 0).
So,
char buf[5]={0,};
is equivalent to
char buf[5]={0,0,0,0,0};
Related Reading : From the C11 standard document, chapter 6.7.9, initalization,
If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have
staticstorage duration.
Yes when you initialize one element in the array to 0 the rest are set to 0
char buf[5] = {0};
char buf[5] = "";
Both are same