Link to home
Start Free TrialLog in
Avatar of hemaraj123
hemaraj123

asked on

Scope Resolution in C

Hai,

I am having 10 modules in a C program, in which all of them are a single project and they are linked. I want to use a Integer type variable in one function, to other by using Scope Resolution Operator, would you please suggest me how can I do that.

Thanks in advance,

Hemaraj123
Avatar of sarda_ramesh
sarda_ramesh

Hi
  Do u wanna use a variable declared in one function in another function??? This is not allowed. To do this u need to have the variable defined as a global variable.
  Please explain ur problem statement a bit.

Regards
Ramesh
Hmmm,

The Scope Resolution Operator (::) is a C++ construct - you can't do it in C...

So, what exactly are you trying to do?

In C, you can have a global variable, or pass it as a function parameter...
Declaring it as a global variable is the easiest way.  However, that allows all modules to change its value.

If you want to do data encapsulation, you can declare it as a static and provide an access function that will return its value.  Needless to say, that access function shouldn't be static.
must agree with gj62 and underline that the scope operator can really only be used in c++ and that the easiest way you can use that same value your searching for is by either making it global or declaring a pointer to it i guess...
Avatar of hemaraj123

ASKER

Hi,

For the above problem, I have also used global variable (declared as extern) in one module and I have used that variable in another module, but that doesn't work either. I will show the coding for that below.

Program name: program1.c

....
extern int var1;

void Truncate(void) {
.....
var1 = 1;
....
}
...

Program name: program2.c
...
void Transpose(void) {
....
int a;
a = var1;
....
}

This also doesn't work. pl. give me some suggestions.

Note:
I am compiling the programs and making exe with "gcc".

thanks

Hemaraj123

ASKER CERTIFIED SOLUTION
Avatar of sarda_ramesh
sarda_ramesh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial