Accessing a global variable when there is a local variable in the same name

M

Mohanasundaram

int i = 10;
int main()
{
int i = 20;
return 0;
}

Hi All,

I want to access the global variable i inside the main. Is there
a way to do this in C?

Regards,
Mohan.
 
M

Martin Ambuhl

Mohanasundaram said:
int i = 10;
int main()
{
int i = 20;
return 0;
}

Hi All,

I want to access the global variable i inside the main. Is there
a way to do this in C?

By not masking it with a redeclaration inside main.
 
J

James Hu

int i = 10;
int main()
{
int i = 20;
return 0;
}

I want to access the global variable i inside the main. Is there
a way to do this in C?

int i = 10;
int main(void)
{
int *global_i_pointer = &i;
int i = 20;

/* ... use global_i_pointer to access the global variable i
from inside main ... */

return 0;
}

-- James
 
C

Chris Dollin

Mohanasundaram said:
int i = 10;
int main()
{
int i = 20;
return 0;
}

Hi All,

I want to access the global variable i inside the main. Is there
a way to do this in C?

There's the easy way and the smartarse way.

The easy way is to rename the local i to something else, or to rename
the global i to something more sensible (a global variable called "i"
is probably a very bad idea).

The smararse way involves using pointers or nested extern declarations.
I'm not going to tell you the details. Just rename one or both i's.
 
V

Vijay Kumar R Zanvar

Mohanasundaram said:
int i = 10;
int main()
{
int i = 20;
return 0;
}

Hi All,

I want to access the global variable i inside the main. Is there
a way to do this in C?

Regards,
Mohan.

int i = 10;
int main(void)
{
int i = 5;
{
extern int i;
i; /* the expression i will evaluate to 10 */
}
return 0;
}
 
D

Dan Pop

In said:
int i = 10;
int main()
{
int i = 20;
return 0;
}

Hi All,

I want to access the global variable i inside the main. Is there
a way to do this in C?

Explain why you can't rename one of the two variables.

If this is a homework question, try finding a better instructor. This
issue should NEVER arise in real C programs.

#include <stdio.h>

int i = 10;

int main()
{
int j = i;
int i = 20;
printf("%d\n", j);
return 0;
}

If the value of the global i might change during the execution of main(),
replace the definition of j by

int *p = &i;

but keep it still *before* the definition of the local i, and use *p any
time you need to access the global i.

Dan
 
K

Keith Thompson

CBFalconer said:
... snip ...

Around here, for a question like this, only the smartarse way is
acceptable.

The obvious way, as others have mentioned, is to change the name of
the local variable. (Some languages have ways to refer directly to
variables in outer scopes using expanded names; C doesn't. I suspect
that's what the OP was really asking about.)

But the smartarse way, using a pointer, does illustrate what might
sometimes be a relevant point: just because a function doesn't have
direct visibility to a variable (either because it's hidden by a
declaration in an inner scope, or because it's a static variable
declared in another file or function), you can't necessarily assume
that the function can't read or modify the variable.
 
E

Emmanuel Delahaye

In 'comp.lang.c' said:
int i = 10;
int main(void)
{
int *global_i_pointer = &i;
int i = 20;

/* ... use global_i_pointer to access the global variable i
from inside main ... */

return 0;
}

Clever. I'll try to keep it in mind.
 
K

Keith Thompson

In <[email protected]>


Explain why you can't rename one of the two variables.

If this is a homework question, try finding a better instructor. This
issue should NEVER arise in real C programs.

#include <stdio.h>

int i = 10;

int main()
{
int j = i;
int i = 20;
printf("%d\n", j);
return 0;
}

If the value of the global i might change during the execution of main(),
replace the definition of j by

int *p = &i;

but keep it still *before* the definition of the local i, and use *p any
time you need to access the global i.

Yes, that will work. It will also create a potential headache for
future maintainers. Code that silently changes behavior when the
order of declarations is changed is not generally a good idea.
 
D

Dan Pop

In said:
Yes, that will work. It will also create a potential headache for
future maintainers. Code that silently changes behavior when the
order of declarations is changed is not generally a good idea.

Which part of "This issue should NEVER arise in real C programs." was too
difficult for you to understand?

Dan
 
K

Keith Thompson

Which part of "This issue should NEVER arise in real C programs." was too
difficult for you to understand?

I understood it quite well, and I agree with it.

You declared that "This issue should NEVER arise in real C programs."
You could have stopped there, but you chose to present a solution.
That's fine. I commented on your solution. I agreed that it would
work, and raised another point that supports your contention that
"This issue should NEVER arise in real C programs."
 
D

Dan Pop

In said:
I understood it quite well, and I agree with it.

You declared that "This issue should NEVER arise in real C programs."
You could have stopped there, but you chose to present a solution.

A solution to a *homework question*, which you arbitrarily chose to treat
as advice about how to write real C programs, otherwise your comment about
future maintainers would be downright idiotic: homework answers have no
future maintainers.

Dan
 
K

Keith Thompson

A solution to a *homework question*, which you arbitrarily chose to treat
as advice about how to write real C programs, otherwise your comment about
future maintainers would be downright idiotic: homework answers have no
future maintainers.

Who said it was a homework question? The OP didn't say so. It's
entirely possible that it was homework; it's also possible that the OP
was trying to find out whether C has some kind of scope resolution
mechanism, as some other languages do.

I didn't treat your solution as advice about how to write real C
programs, but others might. In any case, I was simply expanding on
what you wrote. I don't know why you have a problem with that.
 
D

Dan Pop

In said:
Who said it was a homework question? The OP didn't say so. It's
entirely possible that it was homework;

My own text, prefacing the actual code, made it crystal clear that it was
supposed to be the answer to a stupid homework question and NOT used in
real life C code:

Explain why you can't rename one of the two variables.

If this is a homework question, try finding a better instructor. This
issue should NEVER arise in real C programs.
it's also possible that the OP
was trying to find out whether C has some kind of scope resolution
mechanism, as some other languages do.

And the answer is that it does (as shown in the various posted examples),
but it's better left unused.
I didn't treat your solution as advice about how to write real C
programs, but others might.

Why? Was my disclaimer clear enough for you, but not for others?
In any case, I was simply expanding on
what you wrote. I don't know why you have a problem with that.

Because I hate idiotic expansions on what I write. As I have already
explained, it makes no sense to invoke the future maintainers of something
that is *explicitly* presented as "not to be done in real C programs".

Without my explicit warning, your expansions would have been perfectly
sensible.

Dan
 
K

Keith Thompson

Because I hate idiotic expansions on what I write.

Dan, it's really not that big a deal. What I wrote may not have been
necessary; your overreaction to it certainly wasn't.

In the future, I will spend less time worrying about your opinion
other than on relevant technical issues. I'll probably continue to do
things that make you angry (because there doesn't seem to be any way
to avoid that other than leaving the newsgroup). You can deal with
that in whatever way you choose.
 
M

Mohanasundaram

Hi Dan,

This was an interview question. It is not a homework question. I
just wanted to know whether it is possible to do that like what we do
in C++. We use :: operator in C++ to access the global variable. I
wanted to know is there any language support in C to do this. If you
know please share the answer. If not please ......

I though the answer is either
There is no language support in C
or
There is an operator or something similar to do so like ::

Mohan.
 
M

Mohanasundaram

Hi Thomson,

I feel you make lot of sense and show maturity in the full
thread. You are right and I wanted to know whether C supports any
thing similar to :: for accesing global varibles in C++. It was not a
homework question. I decided to ask this in this group because I had
and argument with an interviewer.

Regards,
Mohan.
 
D

Dan Pop

In said:
Hi Dan,

This was an interview question. It is not a homework question. I
just wanted to know whether it is possible to do that like what we do
in C++. We use :: operator in C++ to access the global variable. I
wanted to know is there any language support in C to do this. If you
know please share the answer. If not please ......

Are you a patent idiot or what? I have already provided the answer.
Or are you unable to read standard C code?

There was no mention of the C++ :: operator in your initial question,
was it?
I though the answer is either
There is no language support in C
or
There is an operator or something similar to do so like ::

And you were wrong. There is language support in C, but it doesn't
involve any operator.

Dan
 
K

Keith Thompson

In <[email protected]>


Are you a patent idiot or what? I have already provided the answer.
Or are you unable to read standard C code?

There was no mention of the C++ :: operator in your initial question,
was it?

The intent of Mohanasundaram's original question was to ask whether C
has an operator that provides access to declarations in outer scopes
that are otherwise hidden. (That wasn't entirely clear from his
original question, but it's very clear now that he's come back and
clarified it.) There are such operators in some other languages (C++
and Perl have "::", Ada has ".", and I'm sure there are plenty of
other examples). The question might have been clearer if he had
mentioned C++'s "::" operator, but this is, after all, a C newsgroup.

The underlying question was actually a very good one, regardless of
the fact that the answer happens to be "no". And I'd recommend to
other posters, particularly newbies, that if you see us going off
track in discussing something you've asked about, please jump in early
and clarify what you really meant.
And you were wrong. There is language support in C, but it doesn't
involve any operator.

The solution Dan Pop provided was not so much a feature of C as a
workaround for the lack of a feature in C. It was a reasonable answer
to the original question as stated, but it didn't answer the intended
question. Yes, as I said, the original question could have been
worded more clearly; it took me a while to figure out the point
myself, and I'm not criticizing Dan for missing it.

I think most of the regulars here have either stopped reading Dan
Pop's postings or have learned to apply a mental "Dan Pop filter",
reading for technical content (which is usually very good) and
ignoring the abuse.

I hope that not too many newbies are put off by Dan Pop's appalling
rudeness, which I expect he'll demonstrate again in response to this
post.
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top