a doubt regarding global vars

S

saurabh

In the fgollowing program I would like to know,Is there a way to access
the gloabal static a,in my functions.
<code>
#include<stdio.h>

static int a=5;


int increment()
{
/* If we add a variable a in increment function it hides the global
* static a
* I would like to know, if there is a way to access the 'global a'*/
int a=1;
a++;
return a;

}
int main()
{

int x=increment();
printf("\n x=%d\n",x);
return 0;

}
</code>
 
L

luserXtrog

In the fgollowing program I would like to know,Is there a way to access
the gloabal static a,in my functions.
<code>
#include<stdio.h>

static int a=5;

int increment()
{
  /* If we add a variable a in increment function it hides the global
   *   static a
   * I would like to know, if there is a way to access the 'global a'*/

/* a rose by any other */
int *name = &a; { /*open new block*/
   int a=1;
   a++;

a += *name; /*should smell as sweet*/
   return a;

}/*clipped thorn*/
}

int main()
{

    int x=increment();
    printf("\n x=%d\n",x);
    return 0;

}

</code>

<obvious>The name is hidden.</obvious>
<consequence?>Access it by other means.</consequence?>
 
F

Flash Gordon

saurabh said:
In the fgollowing program I would like to know,Is there a way to access
the gloabal static a,in my functions.
<code>
#include<stdio.h>

static int a=5;


int increment()
{
/* If we add a variable a in increment function it hides the global
* static a
* I would like to know, if there is a way to access the 'global a'*/
int a=1;

<snip>

The simplest (and best in my opinion) way is to not use the same name
for both variables. If the global really is names "a" then rename it!
The next simplest way, in my opinion, is to pass in a pointer to the global.
 
S

saurabh

..."]
Yes, pass it.

int increment(int hidden_a)
.
.
int x=increment(a);[/QUOTE]

I was thinking of something like scope resolution operator used in C++.
Think of this, how would you tell the two different a's apart? Or what
part of hide don't you understand.

I understand,That I can't tell the difference,nor can the compiler.
That is why I wanted to know if there is something in C (apart from
passing as arg),which lets me differentiate the two 'a's.
 
L

luserXtrog

I was thinking of something like scope resolution operator used in C++.

That's somehow a little more interesting. You *can* grab a global
variable that's otherwise hidden by a local variable, but not if
it's static. I just learned this from:
http://www.lysator.liu.se/c/bwk-tutor.html

#include<stdio.h>

/*static*/ int a=5;

int increment()
{
/* If we add a variable a in increment function it hides the global
* static a
* I would like to know, if there is a way to access the 'global
a'*/
int a=1;
a++;
{ extern int a; printf("extern int a is %d\n", a); }
return a;

}

int main()
{

int x=increment();
printf("\n x=%d\n",x);
return 0;

}


I understand,That I can't tell the difference,nor can the compiler.
That is why I wanted to know if there is something in C (apart from
passing as arg),which lets me differentiate the two 'a's.

Scopes do seem to imply namespaces to some limited extent.
But the overloaded 'static' seems to get in the way.
 
B

Ben Bacarisse

saurabh said:
I was thinking of something like scope resolution operator used in C++.


I understand,That I can't tell the difference,nor can the compiler.
That is why I wanted to know if there is something in C (apart from
passing as arg),which lets me differentiate the two 'a's.

No, but you can copy it or point to it before you hide it:

static int a = 1;

int increment(void)
{
int *outer_a = &a;
int a = 42;
....
*outer_a += a;
...
}
 
B

Ben Bacarisse

luserXtrog said:
/* a rose by any other */
int *name = &a; { /*open new block*/

Snap. I usually read the whole thread before replying, but for some
reason forgot today.
 
K

Kaz Kylheku

In the fgollowing program I would like to know,Is there a way to access
the gloabal static a,in my functions.
<code>
#include<stdio.h>

static int a=5;


int increment()
{
/* If we add a variable a in increment function it hides the global
* static a
* I would like to know, if there is a way to access the 'global a'*/
int a=1;
a++;
return a;

}

Yes. You can introduce a nested scope, and use extern:

{
int a = 1; /* local */

{
extern int a; /* file scope, same linkage as previous */
a++;
}
}

The ``extern'' specifier doesn't mean ``external linkage'' but rather
``same linkage as previously introduced, or else external''.

If you write ``static'', that will make ``a'' a block scope static, and
not reach the file scope ``a''.
 
B

BartC

saurabh said:
In the fgollowing program I would like to know,Is there a way to access
the gloabal static a,in my functions.
<code>
#include<stdio.h>

static int a=5;
int increment()
{
/* If we add a variable a in increment function it hides the global
* static a
* I would like to know, if there is a way to access the 'global a'*/
int a=1;
a++;
return a;
}

I think there is no way of accessing the global a and the local a at the
same time (using those names and in the same scope).

The solutions (or rather, workarounds) you've been offered all introduce
extra scopes so that the two a names are accessed in separate scopes.

The scope resolution method you hinted at in another post doesn't directly
solve the problem either:

a and thismodule::a (or whatever goes in front of ::) effectively use two
different names. But is a more preferable workaround.

I don't think you can do this in C without permanently renaming the global
as thismodule_a (choose your own separator), and you can't then use the
short form a for the global.
 
L

luserXtrog

Snap.  I usually read the whole thread before replying, but for some
reason forgot today.

No harm there. Your version is shorter, prettier, and contains
less red herring (ie. the gratuitous new block).
 
K

Kaz Kylheku

I think there is no way of accessing the global a and the local a at the
same time (using those names and in the same scope).

Yes. Genearlly speaking, a name can have only one binding in the same
environment. (Of course, there can be different /kinds/ of bindings in an
environment, allowing for the same name to have two or more different kinds of
bindings, but in this case, both bindings we are discusssing are of the same
kind: variable name bindings.)

Even scope resolution, if you have it in the language, also introduces an extra
scope.

If you have a syntax like a::b, one way to look at it is that the ``a''
establishes a little tiny scope around the ``b'', with its own environment in
which a has a particular binding.
 
C

Chris Dollin

saurabh said:
In the fgollowing program I would like to know,Is there a way to access
the gloabal static a,in my functions.
<code>
#include<stdio.h>

static int a=5;

int increment()
{
/* If we add a variable a in increment function it hides the global
* static a
* I would like to know, if there is a way to access the 'global a'*/
int a=1;
a++;
return a;

}

Yes. Rename the local `a` to something else.

(This tactic beats every tactic that involves fiddling around with
pointers.)
 
G

Guest

<snip>

The simplest (and best in my opinion) way is to not use the same name
for both variables. If the global really is names "a" then rename it!
The next simplest way, in my opinion, is to pass in a pointer to the global.

or dispense with global data...
 
C

Chris Dollin

Richard said:
Rarely a good idea in the real world. Some data invariably has to be
global.

There's a fairly obvious, if tedious and possibly maintenance-unfriendly,
transformation which will remove all user-defined "global variables" from
a (C) program, which makes "invariably" a little presumptuous.

["Program" includes, obviously, all the not-defined-by-the-standard
libraries that the code might use. I'm not saying that applying this
transformation would be a Universal Good Idea; I'm just saying that
it's existence weakens -- even destroys -- the rhetorical power of
that "invariably".]
 
K

Kenny McCormack

Richard said:
Rarely a good idea in the real world. Some data invariably has to be
global.

I seem to recall a thread where Heathfield claimed there was no such
thing as "globals" in C. Err, yeah, right.

In fact, from the very start of this thread, I've been wondering how
long it would take for one of the regs to point out that, in the
dogmatic religion of CLC, there is no such thing as "global variables".
 
R

Richard Tobin

or dispense with global data...
[/QUOTE]
Rarely a good idea in the real world. Some data invariably has to be
global.

I think this is an exaggeration, as is the opposite view.

On several occasions, I've written programs with global variables,
only to later want to use multiple instances of the program within
another one, with the result that the global variables had to be
collected up into (say) a structure passed as an argument.

(Long-time C programmers will be familiar with this phenomenon in
yacc and lex, where running multiple parser instances suffered
from this problem.)

On the other hand, I still use global variables. Why? Because it's
simpler, and I have many more programs which I've never wanted to use
within others. Sometimes it's more efficient to do things the quick
and easy way, and fix the problem later if you need to.

-- Richard
 
D

David Thompson

In the fgollowing program I would like to know,Is there a way to access
the gloabal static a,in my functions. [where is is shadowed]
Yes. You can introduce a nested scope, and use extern:
I don't believe that's correct.
{
int a = 1; /* local */

{
extern int a; /* file scope, same linkage as previous */
a++;
}
}

The ``extern'' specifier doesn't mean ``external linkage'' but rather
``same linkage as previously introduced, or else external''.
According to 6.2.2p4, that 'previous linkage' feature only applies if
the prior declaration is visible; here it isn't.

Worse, p7 apparently makes this Undefined Behavior. FWIW, of the
compilers I have conveniently at hand, gcc 3.4.5 -Wall -W gives an
error (plus a spurious warning for unused) while MSVC03 /W4 /Za and
AIX xlc (5?) use the file-scope static without peep.
If you write ``static'', that will make ``a'' a block scope static, and
not reach the file scope ``a''.

That's true.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top