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
  • 48,281
  • 17
  • 65
  • 115
  • 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 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
  • 39,972
  • 7
  • 52
  • 94
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621