Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

Simplify numerous array declarations in C

I'm totally new to programming so please bare with me. I got 24 arrays of the same size and I have to call 2 functions with each array as a parameter. So first I tried it like that:

int array1[1023];
int array2[1023];
int array3[1023];
...
int array24[1023];

generateCode(array1, 2, 6);
generateCode(array2, 3, 7);
...
generateCode(array24, 4, 6);

calculate(input, array1, 1);
calculate(inpute, array2, 2);
...
calculate(input, array24, 24);

It works that way but it looks horrible. So my question is how can I simplify that? I googled for hours and I think I need something like a multidimensional array. But these things are messing up my mind on a different level :( Hope someone can help me with this problem.

Thanks in advance!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you could declare an array of array of int bit like this :

int array[24][1023];

generateCode(array[0], 2, 6);
generateCode(array[1], 3, 7);
...
generateCode(array[23], 4, 6); // dunno if you pick random values or if you can make a loop which will do the stuff you want

for (int i = 0; i < 24; i++)
{
    calculate(input, array[i], i + 1); // dunno what does third parameter do so i've put i +1 for tht correspond to your exemple's values but maybe you want something else ;)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...