scope resolution operator???????

E

Erik de Castro Lopo

sushant said:
hi ,

can we use scope resolution operator :):) in C???

No, that is a c++ specific kludge.

Erik

--
+-----------------------------------------------------------+
Erik de Castro Lopo (e-mail address removed) (Yes it's valid)
+-----------------------------------------------------------+
"To me C++ seems to be a language that has sacrificed orthogonality
and elegance for random expediency." -- Meilir Page-Jones
 
D

Derrick Coetzee

sushant said:
can we use scope resolution operator :):) in C???

The only reason to do so would be to access some member of a namespace
or class - neither of which C has. What application did you have in mind?
 
T

thesushant

i want to access the value of global variable inside the main function
and inside the main func the var with the same name is available..for
eg.

int x=20;

main()
{
int x=10;
printf("%d",x);

}
i want the o/p to be 20.. how can v achieve that?
 
A

Andrey Tarasevich

Derrick said:
The only reason to do so would be to access some member of a namespace
or class - neither of which C has.

It can also be used to access a hidden global name from a local scope.
Theoretically, for this very purpose '::' could be useful in C as well.

I'm not saying though that this is enough to justify the need for '::'
operator in C. It is not.
 
B

Ben Pfaff

i want to access the value of global variable inside the main function
and inside the main func the var with the same name is available..for
eg.

int x=20;

main()
{
int x=10;
printf("%d",x);

}

main()
{
int x = 10;
{
extern int x;
printf ("%d", x);
}
...
}
 
T

Taran

i want to access the value of global variable inside the main function
and inside the main func the var with the same name is available..for
eg.

int x=20;

main()
{
int x=10;
printf("%d",x);

}
i want the o/p to be 20.. how can v achieve that?
NO.

Whenever there's a name clash between a local and global variable the
local variable name and value is recognized.
AFAIK there's isn't any way to access global variable with name clashes
with a local vairable. I knew this so I haven't ever used same name in
local scope, my company's coding guidelines suggest we don't do that ;)
, so I never tried accessing the global variable, so I'm not aware even
it exists.

HTH.
Regards,
Taran
 
D

Derrick Coetzee

i want to access the value of global variable inside the main function
and inside the main func the var with the same name is available..

Ah, I didn't think of this. Another reply indicated one way of doing
this. However, this is purely academic, since if this situation ever
arose in practice you should rename one of your variables right away. In
short: don't ever give a global a name that might be reasonably used by
a local variable. If this results in large variable names, you can use
the #define/#undef trick:

int num_furry_bunny_suits = 2;

#define suits num_furry_bunny_suits
void foo(int i) {
suits += i;
return suits;
}
#undef suits

Cleaner but (sometimes) less efficient is a local pointer:

void foo(int i) {
int* suits = &num_furry_bunny_suits;
*suits += i;
return *suits;
}
 
E

Erik de Castro Lopo

i want to access the value of global variable inside the main function
and inside the main func the var with the same name is available..for
eg.

int x=20;

main()
{
int x=10;
printf("%d",x);

}
i want the o/p to be 20.. how can v achieve that?

You can't. In fact the above code should be considered a potential
cause of bugs. If you are lucky enough to be using the GNU C compiler
(gcc) you can avoid these bugs by compiling with the -Wshadow
compiler flag. Add -Werror will turn warnings into errors.

Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo (e-mail address removed) (Yes it's valid)
+-----------------------------------------------------------+
"C++ has its place in the history of programming languages. Just as
Caligula has his place in the history of the Roman Empire."
-- Robert Firth
 
K

Keith Thompson

i want to access the value of global variable inside the main function
and inside the main func the var with the same name is available..for
eg.

int x=20;

main()
{
int x=10;
printf("%d",x);

}
i want the o/p to be 20.. how can v achieve that?

The best way to do it is to change the name.
 
C

CBFalconer

Ben said:
main()
{
int x = 10;
{
extern int x;
printf ("%d", x);
}
...
}

A foul construct indeed. What happens if the local to main x is
declared static? I.e. how does the extern actually get resolved.
 
J

jacob navia

Erik said:
You can't. In fact the above code should be considered a potential
cause of bugs. If you are lucky enough to be using the GNU C compiler
(gcc) you can avoid these bugs by compiling with the -Wshadow
compiler flag. Add -Werror will turn warnings into errors.

Erik

lcc-win32 features a -shadows feature too, exactly like the one
you mention.

This will be automatically turned on when you ask for a "check"
pass of the code.
 
R

Ravi Uday

CBFalconer said:
A foul construct indeed. What happens if the local to main x is
declared static? I.e. how does the extern actually get resolved.
Is the one below at fault ?

bash-2.02$ cat t17.c
#include <stdio.h>

int i = 9;

int main ()
{
static int i = 20;
{
extern int i;
printf ("\nvalue of i decalared outside main = %d\n", i);
}
printf ("\nvalue of i decalared in main = %d\n", i);
return 0;
}


bash-2.02$ gcc -Wall -pedantic t17.c
bash-2.02$ ./a.exe

value of i decalared outside main = 9

value of i decalared in main = 20
bash-2.02$

- Ravi
 
J

Jonathan Burd

sushant said:
hi ,

can we use scope resolution operator :):) in C???

sushant

I can think of one way you can reference that global variable: Use a
getter/setter function for each global variable (kludge).

I've seen code that avoids naming global variables the same as local
variables by using conventions like g_variable_name for global variables
and not using any prefix (at least not g_) for any local variables.

However, this is clearly not ideal. I'd avoid using global variables as
much as possible. Just my two cents.

Regards,
Jonathan.
 
L

Lawrence Kirby

Ben Pfaff wrote:
....


A foul construct indeed. What happens if the local to main x is
declared static? I.e. how does the extern actually get resolved.

Block scope object identifiers (except declared with extern) have no
linkage, even static ones. So a block scope static int x; is not a
candidate for linking to an extern int x;

If you try to mix internal and external linkage for the same identifier in
the same translation unit you get undefined behaviour.

Lawrence
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top