Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Scope Resolution in C

Posted on 2003-03-25
6
Medium Priority
?
504 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:hemaraj123
6 Comments
 
LVL 1

Expert Comment

by:sarda_ramesh
ID: 8208482
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
0
 
LVL 6

Expert Comment

by:gj62
ID: 8210608
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...
0
 

Expert Comment

by:MHuang
ID: 8211257
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Expert Comment

by:fpierre0629
ID: 8211737
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...
0
 
LVL 1

Author Comment

by:hemaraj123
ID: 8215831
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

0
 
LVL 1

Accepted Solution

by:
sarda_ramesh earned 60 total points
ID: 8216436
hi there,
  try doing it as below.It will work.
ramesh


Program name: program1.c

int var1;
....


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

Program name: program2.c

extern int var1;

...
void Transpose(void) {
....
int a;
a = var1;
....
}

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question