Declaring and initializing arrays in C

前端 未结 8 2132
北海茫月
北海茫月 2020-12-01 03:15

Is there a way to declare first and then initialize an array in C?

So far I have been initializing an array like this:

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


        
8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-01 04:03

    Is there a way to declare first and then initialize an array in C?

    There is! but not using the method you described.

    You can't initialize with a comma separated list, this is only allowed in the declaration. You can however initialize with...

    myArray[0] = 1;
    myArray[1] = 2;
    ...
    

    or

    for(int i = 1; i <= SIZE; i++)
    {
      myArray[i-1] = i;
    }
    

提交回复
热议问题