This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Initializing C arrays with repeated values



Hello Community!

I was trying to optimize the task of initialize large arrays in C for my project on C64+.  For example:

int largeArray[10] = {1, 1, 1, 1, 1,1, 1, 1, 1, 1}; // solution 1

I believe that "copy and paste" is not the better way to initialize the largeArray variable. Then I browse the Internet and found this solution (only works with GCC compiler)

int largArray[10] = { [0 ... 9] = 1}; // solution 2

Another solution(which will consume processing cycles from my app) is build a routine for initialize the array:

int largeArray[10]; // solution 3

for(int i=0;i<10;i++){

largeArray[i] = 1;

end

Then, there is a method similar to "solution 2" available in C compiler for C64+?

 

Thanks in advance.

 

By the way, for curiosity, in Python you can do the job in the most efficient way:

largeArray = [ 1 ] * 10

  • Ronaldo Viera said:

    Then I browse the Internet and found this solution (only works with GCC compiler)

    int largArray[10] = { [0 ... 9] = 1}; // solution 2


    Support for this syntax can be enabled in the TI compiler by using the --gcc compiler option.