Search top init quick&q 0988574006&tas 0.2784707673097184 năm 2024

There are different ways through which we can do this, so we’ll list them all one by one. Let’s get started!


An initializer list initializes elements of an array in the order of the list.

For example, consider the below snippet:

int arr[5] = {1, 2, 3, 4, 5};

This initializes an array of size 5, with the elements {1, 2, 3, 4, 5} in order.

This means that

int arr[5] = {1, 2, 3};

6,

int arr[5] = {1, 2, 3};

7, and so on.

We don’t need to initialize all the elements 0 to 4. We can even do only from indices 0 to 2.

The following code is also valid:

int arr[5] = {1, 2, 3};

But now,

int arr[5] = {1, 2, 3};

8 and

int arr[5] = {1, 2, 3};

9 will still remain garbage values, so you need to be careful!

If you’re using an initializer list with all elements, you don’t need to mention the size of the array.

// Valid. Size of the array is taken as the number of elements
// in the initializer list (5)
int arr[] = {1, 2, 3, 4, 5};
// Also valid. But here, size of a is 3
int a[] = {1, 2, 3};

If you want to initialize all the elements to 0, there is a shortcut for this (Only for 0). We can simply mention the index as 0.


# include 
int main() {
    // You must mention the size of the array, if you want more than one
    // element initialized to 0
    // Here, all 5 elements are set to 0!
    int arr[5] = {0};
    for (int i=0; i<5; i++) {
        printf("%d\n", arr[i]);
    }
    return 0;
}

Output

0
0
0
0
0

If you’re using multi-dimensional arrays, you can still initialize them all in one block, since arrays are stored in a row-wise manner.


# include 
int main() {
    int arr[3][3] = {1,2,3,4,5,6,7,8,9};
    for (int i=0; i<3; i++)
        for (int j=0; j<3; j++)
            printf("%d\n", arr[i][j]);
    return 0;
}

Output

1
2
3
4
5
6
7
8
9

A similar method can also be used for other datatypes, like

// Valid. Size of the array is taken as the number of elements
// in the initializer list (5)
int arr[] = {1, 2, 3, 4, 5};
// Also valid. But here, size of a is 3
int a[] = {1, 2, 3};

0,

// Valid. Size of the array is taken as the number of elements
// in the initializer list (5)
int arr[] = {1, 2, 3, 4, 5};
// Also valid. But here, size of a is 3
int a[] = {1, 2, 3};

1,

// Valid. Size of the array is taken as the number of elements
// in the initializer list (5)
int arr[] = {1, 2, 3, 4, 5};
// Also valid. But here, size of a is 3
int a[] = {1, 2, 3};

2, etc.


# include 
int main() {
    // Array of char* elements (C "strings")
    char* arr[9] = { "Hello", [1 ... 7] = "JournalDev", "Hi" };
    for (int i=0; i<9; i++)
        printf("%s\n", arr[i]);
    return 0;
}

Output

Hello
JournalDev
JournalDev
JournalDev
JournalDev
JournalDev
JournalDev
JournalDev
Hi

Remember, this method with [1 … 7] = “Journaldev” might not work with all compilers. I work on GCC in Linux.

We can also use the

// Valid. Size of the array is taken as the number of elements
// in the initializer list (5)
int arr[] = {1, 2, 3, 4, 5};
// Also valid. But here, size of a is 3
int a[] = {1, 2, 3};

3 loop to set the elements of an array.


# include 
int main() {
    // Declare the array
    int arr[5];
    for (int i=0; i<5; i++)
        arr[i] = i;
    for (int i=0; i<5; i++)
        printf("%d\n", arr[i]);
    return 0;
}

Output

int arr[5] = {1, 2, 3};

0

If you’re using

// Valid. Size of the array is taken as the number of elements
// in the initializer list (5)
int arr[] = {1, 2, 3, 4, 5};
// Also valid. But here, size of a is 3
int a[] = {1, 2, 3};

4 as your C compiler, you can use designated initializers, to set a specific range of the array to the same value.

int arr[5] = {1, 2, 3};

1

Note that there is a space between the numbers and there are the three dots. Otherwise, the compiler may think that it is a decimal point and throw an error.

int arr[5] = {1, 2, 3};

2

Output (for gcc only)

int arr[5] = {1, 2, 3};

3

We can also combine this with our old initializer list elements!

For example, I am setting only array index

// Valid. Size of the array is taken as the number of elements
// in the initializer list (5)
int arr[] = {1, 2, 3, 4, 5};
// Also valid. But here, size of a is 3
int a[] = {1, 2, 3};

5 as 0, while the others are designated initialized to 10!

Can you initialize variables in a struct C++?

C99 and C++ allow the initializer for an automatic member variable of a union or structure type to be a constant or non-constant expression. The initializer for a static member variable of a union or structure type must be a constant expression or string literal.

How do you initialize a struct member?

Initialization using Designated Initializer List. Designated Initialization allows structure members to be initialized in any order. This feature has been added in the C99 standard. struct structure_name str = { .member1 = value1, .member2 = value2, .member3 = value3 };