简体   繁体   中英

How to make this variable available to the main function efficiently?

I am doing some calculations, and I need to write a few large arrays (~100 elements each) by hand. They look like this:

int arr[] = {3, 4, 5,
             4, 9,
             3, 4, 6, 9};

Having multiple rows like this is important for readability, because the rows in the array correspond rows of a matrix.

To not clutter the main function, I would like to put these arrays in a header file. As far as I know, there are two options:

  1. Make these arrays global variables in the header file. I'd like to avoid global variables if possible.

  2. Make a function in the header file, like this:

     void data(int *arr) { arr[0] = 3; arr[1] = 4; arr[2] = 5; arr[3] = 4; arr[4] = 9; arr[5] = 3; arr[6] = 4; arr[7] = 6; arr[8] = 9; }

and pass in arr from the main function. But this is very ugly, and time-consuming to write.

Is there any other solution?

A solution just occurred to me, I can write the arrays as "global variables" inside the header file, but #include the file in the main function rather than at the top of the program.

You approach would differ depending on the logic of your program, and also whether you are going with C or C++. (In most cases people will get very angry if you tag both. see )

If you have a set of many arrays, predefined at compile time, ie, contains some data at the start of your program, you can define them in a header file with reasonable names. If you wish, you can prefix them with an identifier, like myMats_mat1 , for your matricies to avoid naming conflicts. C++ also presents a native way to accomplish this by namespace s.

Normally though, you would have a cleaner, neater logic. For instance, if you have a large amount of data, like your many arrays with hundreds of elements, you usually would store that in a file (a regular text file, or a binary file), and have functions to read it and write back to local containers when they are needed. This also allows you to both separate your data from your code, and occupy less memory since you only would work with a presumably smaller subset of the data.

In most cases, you would also have multiple functions to do stuff with these arrays, perhaps you would have a function to compare two matricies, another to take determinant, another to sum all elements, whatever. You would also group these together in seperate .h and .c files together with your arrays (in the .h file). Again depending on your program, you may not even need to access the arrays themselves in your main function, and just call functions to do stuff on them. In such case, you can define your arrays as static , restricting their scope to that particular file, hence not really putting them global scope.

In C++, you can also group them in a class, both your arrays and the related functions. Its basically the same idea as above, just the C++ way of implementing it.

I should stress that the ideas that I throw out there may be good, acceptable or outright horrible depending on your use case. Take all with a grain of salt.

you can write in any file even an csv then you can use excel open and edit it,only number,like

1,2,3,4,
5,6,7,8,
2,4,4,4

then in c file like

int arr[] ={
#include"a.csv"
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM