Until, explicitly changed to 0, array will likely contain random/garbage (indeterminate) value.
What are default values of array elements ?
char arr[12]; // Elements initialized to 0
static char arr[12]; // Elements initialized to 0
void function(void)
{
char arr[12]; // Elements have indeterminate value
static char arr[12]; // Elements initialized to 0
}
What happens if array is not initialised ?
int main()
{
char arr[64];
printf("What arr[34] contains %d\n",arr[34]);
if (strlen(arr) != 0) {
printf(" Array is not initialised \n So it might be filled with junk values \n Eg : arr[2] %d\n",arr[2]);
}
return 1;
You will have random contents in all the elements of arr. Here, the array arr is declared and defined. But not intialized. An array must be initialized at the time of definition. Intialization is not only specific to array, but for all the variables.
There are various ways to initialize a local array (non-static) :
1. Array Initialization, while declaring
a)
int main()
{
char arr[64] = {0};
printf("What arr[34] contains %d\n",arr[34]);
if (strlen(arr) == 0) {
printf(" Array is initialised \n But with fewer initializers \n Eg : arr[32] %d\n",arr[32]);
}
return 1;
2. Memsetting
This is good/preferred way of array initialization.
What are default values of array elements ?
char arr[12]; // Elements initialized to 0
static char arr[12]; // Elements initialized to 0
void function(void)
{
char arr[12]; // Elements have indeterminate value
static char arr[12]; // Elements initialized to 0
}
What happens if array is not initialised ?
int main()
{
char arr[64];
printf("What arr[34] contains %d\n",arr[34]);
if (strlen(arr) != 0) {
printf(" Array is not initialised \n So it might be filled with junk values \n Eg : arr[2] %d\n",arr[2]);
}
return 1;
}
What arr[34] contains -15
Array is not initialised
So it might be filled with junk values
Eg : arr[2] 43
You will have random contents in all the elements of arr. Here, the array arr is declared and defined. But not intialized. An array must be initialized at the time of definition. Intialization is not only specific to array, but for all the variables.
1. Array Initialization, while declaring
a)
int main()
{
char arr[64] = {0};
printf("What arr[34] contains %d\n",arr[34]);
if (strlen(arr) == 0) {
printf(" Array is initialised \n But with fewer initializers \n Eg : arr[32] %d\n",arr[32]);
}
return 1;
}
What arr[34] contains 0
Array is initialised
but with fewer initializers
Eg : arr[32] 0
In the example above, this means that the first element of arr is explicitly set to 0, while all the remaining elements are implicitly set to 0. If there are fewer initializers, the remaining of the array is intialized with 0. There will be no random content in this case. So, this is not the correct idea to initialize.
These are equivalent :
char arr[64] = "";
char arr[64] = {0};
char arr[64]
= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
char arr[6] = "String";
is valid and same as
char p[6] = {'S', 't', 'r', 'i', 'n', 'g'};
These are equivalent :
char arr[64] = "a"; char arr[64] = {'a'}; char arr[64] = {'a', 0, 0, 0, 0, 0, 0, 0, 0, 0};
2. Memsetting
This is good/preferred way of array initialization.
memset(arr, '0', sizeof(arr));
Global Array Initialization
To be written
Reference :
https://stackoverflow.com/questions/12750265/what-is-the-default-value-of-members-of-a-char-array-in-c
Global Array Initialization
To be written
How to check an array is intialized with 0 or not ?
To be writtenReference :
https://stackoverflow.com/questions/12750265/what-is-the-default-value-of-members-of-a-char-array-in-c
Comments
Post a Comment