0
int a[5] = {2,4,6,8,10}; // Works

But,

int a[5];
a = {2,4,6,8,10}; //Doesn't Work

Why?

Is there any other way in which I can initialize a array in a single go which is created before.

Jabberwocky
  • 45,262
  • 17
  • 54
  • 100
  • 1
    Does this answer your question? [https://stackoverflow.com/questions/3137671/declaring-and-initializing-arrays-in-c](https://stackoverflow.com/questions/3137671/declaring-and-initializing-arrays-in-c) – Francesco Lucianò May 08 '20 at 08:20
  • 1
    The first is an initialization, not an assignment. The second attempts to assign an initializer to an array (as an executable assignment), which isn't supported in C. – Tom Karzes May 08 '20 at 08:20
  • https://stackoverflow.com/questions/8886375/possible-to-initialize-an-array-after-the-declaration-in-c – beynDestroyer May 08 '20 at 08:21

1 Answers1

1

I haven't tested it myself, but since C99 you should be able to use a compound literal with memcpy to copy the data into the array:

memcpy(a, (int[5]){2,4,6,8,10}, sizeof a);
David Ranieri
  • 37,819
  • 6
  • 48
  • 88
Some programmer dude
  • 380,411
  • 33
  • 383
  • 585