To initialize a C array

Asked 1 years ago, Updated 1 years ago, 127 views

I want to initialize the same value from beginning to end when declaring an array.

Now

int myarr[30];
for(i=0; i<30; i++)
    myarr[i] = 3;

I'm doing a for statement like this, but not memset() myarr[all] = 3; Isn't there a way to set it up all at once like this?

c array initialization array-initialize

2022-09-22 22:22

1 Answers

Sometimes the value you want to initialize is 0, but no other value. To initialize to a specific value, you need to circle the for statement or specify it one by one.

int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };

int myArray[10] = { 1, 2 }; -> Indexes 2 through 9 have 0 values

int myArray[10] = { 0 };

int myArray[10] = {};

static int myArray[10]

The zero here does not mean that all of the memory space is set to zero from beginning to end (char is \0 etc.), so it is a bit different from memset().


2022-09-22 22:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.