-3

In how many ways we can declare an array in C programming? if there are many ways to declare an array in C, what are the best practices or best way among?

So far I have been initializing an array like this:

int myArray[SIZE] = {1,2,3,4....};

What are the other ways do the same?

Prabhakar Undurthi
  • 6,272
  • 2
  • 39
  • 50

2 Answers2

2

From C99, you can also use explicit indexes, called designators, in the initializer expression, which is sometimes very nice:

const int threetoone[] = { [2] = 1, [1] = 2, [0] = 3 };

The above is the same as

const int threetwoone[] = { 3, 2, 1 };
unwind
  • 378,987
  • 63
  • 458
  • 590
0

datatype arrayName[arraySize];

int x[10];
int x[]={1,2,3,4,5,6,7,8,9,0};

you can look at this question for more details on ways to initialize an array in C declaring-and-initializing-arrays-in-c

Community
  • 1
  • 1
Himanshu Sourav
  • 679
  • 1
  • 8
  • 31