-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
  • 6,458
  • 2
  • 40
  • 51
  • Check this link: http://www.tutorialspoint.com/cprogramming/c_arrays.htm. BTW, it's the first google search result when searching for "C arrays" – Cristik Apr 17 '15 at 09:42
  • You should trust what it says there :) – Cristik Apr 17 '15 at 09:44
  • I know that link. That's not what am looking for. I need to know how many ways are there to declare an array in C ? – Prabhakar Apr 17 '15 at 09:44
  • 2
    There is only one way to *declare* an array. The are a couple of ways of *initializing* it though. One way you show, the other is setting each element one by one. Or a loop or `memset` if you want to set all elements to the same value. – Some programmer dude Apr 17 '15 at 09:44

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
  • 391,730
  • 64
  • 469
  • 606
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
  • 700
  • 1
  • 10
  • 35