last day (15 days later) » 

10:30 AM
-1
Q: Change variables in place in void function without arguments

sequenceI'm currently porting a program from Fortran to C and I want to declare some global variables for the entire program, which consists of several .c and .h files, so that the said variables could be used not only in the file where they are declared, but in other files as well. The problem is that t...

 
GammaL = malloc(...) should be *GammaL = malloc(...) Same with GammaR.
 
#define DEF_ARGS double **GammaL, double **GammaR, double **GammaT and then void fill_array(DEF_ARGS) {...}. But why do you need to pass arguments if they are global for the whole program? Can you just use it inside a function without args?
 
@DavidRanieri That's my question. It doesn't seem that I can "globally" fill the arrays in place in a void function without arguments. Or can I?
 
To avoid a long list of argument to your function, you could do do something like void fill_array(double ***Gamma), Gamma pointing to an array containing all your previous arguments, but this might become annoying to handle.
 
@sequence of course you can (if the unit where the functions are defined includes the extern variables)
 
10:30 AM
@DavidRanieri If I write code like this, will it actually change the arrays in place? void get_bcs() { GammaL = malloc(nequ*sizeof(double)); for (int i=0; i<nequ; i++) { GammaL[i] = BCgammal[i]/(0.5*BCalfal[i]-BCbetal[i]*rdx); } } It doesn't look like I'm changing by reference here.
 
@sequence since they are pointers to pointer you need to dereference: *GammaL = ...
 
It is not at all clear what you are asking or why. If the arrays are global, why pass them as arguments? (Although a better question is why are you using globals!). If all the arrays are a fixed length (4 doubles), why dynamically allocate them? You are making your own problems, and this looks like an X-Y problem - we could help you implement your solution, but we rather address the real problem.
 
@Clifford As far as I understand passing them by arguments (by reference) is necessary so that they could be changed in place in the void function. The integer 4 is just an example, they could be of a different length. I'm using globals to make the variables accessible in more than one file. So that instead of declaring the arrays several times I could reuse them and declare only once. That's basically what I want to do. Sorry I'm still new to C.
 
@sequence The code in your own comment seems fine. I'm not sure what you mean by " It doesn't look like I'm changing by reference here" but the code will for sure update the value of both the pointer and the pointed to objects
 
Why are your variables global in the first place? To avoid passing them as arguments to all functions that use them, am I right? Why is fill_array different in this respect?
 
10:30 AM
@sequence your understanding about "passing the address in order to change the value" is not true for global variables (but would be true if it was a variable with automatic storage duration - which again is better than global but that's another story)
 
@4386427 Thanks. I'm sorry, I'm coming from Python and Fortran where these things work somewhat differently so I'm confused sometimes with C as I'm still new.
@n.m. That's right, they are global so that, once filled, they could be used in other files which make up this program. Can you please clarify what you mean by fill_array being different in this respect?
@4386427 So global variables cannot be changed by passing them by reference and have to be changed the way I did in the code in my comment above? Thanks.
 
@sequence If you have a local variable X in function foo (i.e. a variable with automatic storage duration) and you want to have a function bar that can changes the value of X then it is correct that you must pass the address of X as an argument to bar. However, global variables can be changed from any function without passing any arguments.... (but global variables are in general bad - so try to avoid them)
 
@sequence : re arrays of different length, then your question is dangerously misleading (and has caused me to delete my answer) - I hope the real code is not like that. Why not post the real code in any case? Your reasoning for use of globals and argument passing is seriously flawed too.
 
You are asking how to avoid passing your variables to fill_array as arguments. I presume you are not passing these variables to all other functions that use them, otherwise it would not make any sense to make them global. You have many functions that use your variables without having them in the parameter list. Why is fill_array having them?
 
@sequence Well, you can change a global variable by passing its address to a function but why would you do that? You can access it directly...
 
10:30 AM
@4386427 Thanks. I will read more on global variables in C. I'm porting a Fortran program which uses lots of global variables. Would it be more advisable to declare the arrays again and again every time I need them?
@n.m. That's because I thought that it wouldn't be possible to fill the arrays in place without passing them to fill_array by reference, but 4386427 has straightened this out now.
 
Why do you think the overall structure of your C port needs to be any different than the structure of the original Fortran program? There is nearly 1:1 correspondence between C and Fortran globals, functions, function parameters, and so on. Not exactly 1:1 but I din't see where exactly you are running into the differences between the languages
 
This is certainly an X-Y problem. You have a solution in mind, can't implement it, and are asking about that. You would do better to ask a question about the problem this is a solution to.
 
@n.m. I wish if I could leave that intact, but in Fortran I have the following: real, dimension(nequ) :: GammaL, GammaR, GammaB, GammaT parameter ( GammaL = BCgammal/(0.5*BCalfal-BCbetal*rdx) ) (two lines). So in Fortran this is declared in a module and then in files and functions where these arrays are used I simply write use problemsize to make these arrays available. I don't think I can make it this way in C.
@Clifford If I put my question in a shorter form, it would be this: "How do I make non-constant arrays global in C?", where by non-constant I would mean arrays that need to be filled with some data dependent on other variables.
@n.m. Can you please post your comment, which solved my problem, about the global variables, as an answer? I'd appreciate that.
 

last day (15 days later) »