-1

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Helix
  • 103
  • 1
  • 5
  • *make a function in the header file*, you should not do that. You should only put the function prototype in the header file. – alex01011 Feb 23 '22 at 00:14
  • You could write some meta-code to generate the code for the array. – dbush Feb 23 '22 at 00:16
  • @alex01011 Are you talking about this particular example, or in general? – Helix Feb 23 '22 at 00:19
  • In general, you should avoid it. – alex01011 Feb 23 '22 at 00:20
  • Another solution could be to have a function generate the array, and then return it. – alex01011 Feb 23 '22 at 00:20
  • @dbush Unfortunately that is way beyond my current knowledge. – Helix Feb 23 '22 at 00:21
  • @alex01011 Ahh very nice, I didn't know you could "save" a local variable in that way. Why shouldn't you make functions in header files? Won't the source file become too large and difficult to read if it's filled with all the function definitions? – Helix Feb 23 '22 at 00:23
  • I'm not saying that you shouldn't use headers. The "right" way is, put all declarations in header files e.g `my_header.h` and the place the actual implementation in `.c` file in this case `my_header.c`. This way you don't have to recompile whatever code is in the header whenever you make a change somewhere else in your code. – alex01011 Feb 23 '22 at 00:29
  • When you place a definition in a header file, every includer of the header file gets the definition. If you have two cpp files containing the header, you now have two copies of the definition. The linker, the tool that puts the pieces together once the compiler's finished compiling the pieces, won't even try to figure out which is the real definition. It just spits out an error message telling you to fix it. – user4581301 Feb 23 '22 at 00:30
  • You can place a function in a header, sometimes you just gotta do it [to solve some types of problems](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file), but you have to mark the function as `inline` so that the tools know to handle the function a bit differently and not produce multiple copies. – user4581301 Feb 23 '22 at 00:31
  • Handy reading: [How does the compilation/linking process work?](https://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work) – user4581301 Feb 23 '22 at 00:33
  • @alex01011 I know this is probably not what you're saying, but this is what I'm understanding: Suppose my main goal is to run a function `C`, which needs two smaller functions `A` and `B` to be run first. We should have a main source file `main.c`, two header files `headerA.h`, `headerB.h`, with prototypes for `A`, and `B`, two files `sourceA.c`, `sourceB.c` with actual code. Then `#include#` all these 4 files in `main.c`, and finally run `main.c`. – Helix Feb 23 '22 at 01:01
  • @user4581301 Thanks, that makes a lot of sense. But I don't understand exactly how we should split things up. Suppose your main goal is to run a function `C`, which needs two smaller functions `A` and `B` to be run first (maybe they are even supposed to be called inside of `C`). But you don't want to include `A, B, C` in one giant file. How would you split it up into headers and source files? – Helix Feb 23 '22 at 01:05

3 Answers3

0

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.

Helix
  • 103
  • 1
  • 5
  • the reason people say 'dont put in a header' is becuase typically headers are included in multiple source files, thats what they are for. Its fine if you only intend to include one. Reminder you can call it "array.def" if you want - it doesnt have to be 'array.h' – pm100 Feb 23 '22 at 00:23
  • @pm100 Thank you for the explanation. So are you supposed to write out all your functions in the source files? What do headers typically contain then? I'm sorry for the naive question, I don't know too much about programming. I only write programs with a single source file to display some calculations in the console. I only use header files to put functions in them so that the source file is simpler. – Helix Feb 23 '22 at 00:31
  • You could have them in the same file as `main` but not actually inside `main` – M.M Feb 23 '22 at 00:32
  • headers typically contains declarations of functions (their names and paramters not the implemenations), constant, decalrations of structs, etc. macros. No executable code. Go and look at string.h for example – pm100 Feb 23 '22 at 00:46
0

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, i.e., 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 namespaces.

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.

aulven
  • 521
  • 3
  • 14
0

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"
};
layty
  • 31
  • 6
  • It would make much more sense to create functions to read that file and write them to arrays instead of almost abusing the preprocessor like this. – aulven Feb 23 '22 at 01:11
  • yes , we can use csv file and use fread to parse – layty Feb 28 '22 at 11:21