. Advertisement .
..3..
. Advertisement .
..4..
There are diverse ways of working with arrays in C that you might have been confused about the way to initialize a C array to 0. This blog is a guideline for you!
An array in C is a data structure that can be a storage for the same elements with fixed-size and ordered arrangement. While you might have heard about C arrays as the usage for storing data collection, they would be better utilized for collecting same-type variables.
In C, an array declaration will not require you to declare separately variables like number0, number1, etc. Instead, you will declare array variables. For example, to declare a number array variable, you can use numbers[0], numbers[1],..,numbers[99] to indicate separately variables.
In this guide, you will learn to initialize a C array to 0 with the below given C array declaration.
Initialize a C Array to 0
char ZEROARRAY[1024];
The array declaration will turn zero at runtime. In case the array is local, you can use a shorthand solution. You can follow how to declare and initialize it as the following guide:
char ZEROARRAY[1024] = {0};
The compiler fills up the entries that do not contain zeros. When you can only initialize an array partially, elements that can not be initialized would receive the zero value of related data types.
The compiler fills up the entries that do not contain zeros. If you do not see any specified initializer, the static stored objects will be initialized to 0. In this case, the array declaration will happen as below:
static int myArray[10];
You can initialize a C array to 0 only when there is an empty initializer list or a specified one for 0. The instruction for declaring is as below:
int number[5] = { };
int number[5] = { 0 };
Initialize a C Array to 0 by Using Loops
Loop is the easiest and simplest method to initialize the arrays to 0. In C, in general, you can use a loop to implement a code block several times based on the given condition. This indicates that the loop conducts the same code many times to save code and traverse the same array of elements.
In this blog, the for Loop is used for initializing the C array to 0. The below code example shows that the loop goes through the elements and turns them 0 (The number of iterations is 5).
#include <stdio.h>
int main(void)
{
int numberArray[10], counter;
for(counter = 0 ; counter < 5 ; counter++)
{
numberArray[counter] = 0;
}
printf("Array elements are:\n");
for(counter=0; counter<5; counter++)
{
printf("%d",numberArray[counter]);
}
return 0;
}
Output:
Array elements are:
00000
Initialize a C Array to 0 by Using the memset() Library Function
Memset() is a “string.h” library function that is used for filling a memory block with a certain value. You can apply this function in cases you want to fill some or all memory blocks with a given value. Below is the memset () syntax.
void *memset(void *pointerVariable, int anyValue, size_t numberOfBytes);
Notes about the memset() library function:
- PointerVariable is the variables pointing to the memory blocks to fill.
- AnyValue is the set value that is in integer. However, this function will use the converted and unsigned char to fill the memory block.
- NumberOfBytes is the bytes amount that is set to value. This way, a pointer to the pointerVariable memory would be returned thanks to this function.
You can refer to the detailed coding tutorial as follows:
#include <stdio.h>
#include <string.h>
void printArrayvalues(int anyArray[], int anyNumber)
{
int index;
for (index=0; index<anyNumber; index++)
printf("%d ", anyArray[index]);
}
int main(void)
{
int number = 10;
int arrayValues[number];
memset(arrayValues, 0, number*sizeof(arrayValues[0]));
printf("Array after memset()\n");
printArrayvalues(arrayValues, number);
return 0;
}
Output:
Array after memset()
0 0 0 0 0 0 0 0 0 0
Initialize a C array to a Value Another Than 0
You can use gcc to initialize a C array to a value another than 0 as the below demonstration:
int myArrayValues[1024] = { [ 0 ... 1023 ] = -1 };
Also, each array member can be initialized obviously by skipping the dimension. To declare it, you can refer to the below guide:
int myArrayValues[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
The dimension will be deduced by the compiler from an initializer list. With a multidimensional array, you can only skip the outermost dimensions.
int myPoints[][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9} };
Conclusion
Now you have been introduced to the use of some functions to initialize a C array to 0. If you are interested in declaring an array in the programming field, here is another suggested post for you to learn more.
Leave a comment