shared variables in C. Need to access same variable from two .c files
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What do you mean? Can you give an example of what you are trying to do? If you mean to access a variable that is defined within one compilation unit from another one, you can use the extern keyword:
edit: The extern keyword declares a variable (but does not define it). It is similar to a function prototype. It tells the compiler when main.c is being compiled that there is a variable called externalVariable, which is of type int and will be available during linking. A variable may be declared multiple times with extern, but at least (and not more than) once defined somewhere.
I think that's what I need. I need to fill an array from class1.c, and I need to access that array from both class1.c and class2.c, so I'll try that. thank you very much.
is it necessary to create variable.c? because I fill the array in class1.c and not in variable.c, is it neccesary to have the same name variable.c and variable.h?
how do I have to do to share a variable that need to modify in one file and need to access from another?
...
That would be an array, just that.
Pretty much answered by millgates, but I think I can say it more clearly:
You define your variable as a global in one module and declare it as extern in every other module that uses it.
There is no keyword for global. A variable in C is global if you define it outside of any function and you do not use the keyword static. So:
Code:
int myArray[23];
If that is inside a function definition, it is an ordinary (automatic) variable with scope in just that function. But the same line outside of any function defined a global array.
In other modules
Code:
extern int myArray[23];
In big projects, various header file methods are appropriate to help keep such declarations in sync across all modules using them. The answer by millgates assumes you will want to get in the habit of doing it that way from the start, even if you currently have only two modules.
Edit: I was answering while you were asking more.
Quote:
Originally Posted by franmf
is it necessary to create variable.c? because I fill the array in class1.c and not in variable.c, is it neccesary to have the same name variable.c and variable.h?
No. All those details are just there to make maintenance of large projects practical. In a small project, doing things that way would only be to get in the right habits for later large projects (so you might want to, but that is your decision, not ours).
is it necessary to create variable.c? because I fill the array in class1.c and not in variable.c, is it neccesary to have the same name variable.c and variable.h?
You may name those files any way you like. I just used this as an example. You just define the variable in one file without extern and then with extern in the file from which you want to access it.
But it all depends on what exactly your code looks like. You may avoid the need for using extern alltogether if you redesign your code. You may also just pass the pointer to the array to the other function as parameter. But I don't know what your program looks like and what are you trying to do in the first place, so it's hard to tell.
So class1 and class2 or whatever they're called are two separate binaries? Then I really don't understand what you are trying to achieve. You want to access a variable in one program from another one?
knock and knockd are two .c files in the same directory. Both have their own main, but apart from that, I'd need to share a variable between them. maybe it can't be done and I have to think another way to do such
In that case that cannot be solved by extern. That would have worked if class1.c and class2.c were compiled into a single binary. In your case, you will have to use some sort of inter-process communication. It is not trivial topic, though. Is there a reason why you can't do what you want to do in one program?
#ifndef _GLOBAL_H_
#define _GLOBAL_H_
int ip_status[10];
char ips2[10][30];
#endif
include "variable.h"
and just call them as if they were declared
it compiles.
thank you. Tomorrow morning I'll see if it works because I need the router which is not here righ now.
Actually, this is not exactly the same as what I posted before.
You mean you just include that in both ".c" files? That will certainly compile, but won't work because each program will get its own, independent variables.
how do I have to do to share a variable that need to modify in one file and need to access from another?
We initially misunderstood the word "file" in that request.
The answers assumed "file" meant .c file within one program. But now we understand it means two separate programs.
I would tend to think that means two programs running together on one computer, so shared memory is the logical answer.
But:
Quote:
Originally Posted by franmf
I'll see if it works because I need the router which is not here righ now.
That implies two programs running on different computers.
Please clarify what you actually need to accomplish.
For programs running on different computers, you can't actually share a variable. But you can get roughly the same results as a shared variable would have allowed, using methods such as MPI.
first of all sorry for my english, it may be confusing.
what I have are two programms running at the same time in the router. So, while one of them is running, I use the other to input external data with scanf. So when I insert that data the already running programm does one thing or another depending on the new data I have just inserted.
I don't know if I explain myselft...sorry for that.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.