?
Solved

Scope Resolution in C

Posted on 2003-03-25
6
Medium Priority
?
500 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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!

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

777 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