Question on Static Global variable

C

C_Programmer

Question#1:
==========
==========
What is the default value for any Static Global variable?

Example:

A.c:
=====

static int i;



Does "i" get to initialized to "0"?



Question#2:
==========
==========
Can I access a Static Global variable from a different file as given in
the following example?

Example:


A.c:
=====

static int i;

int * foo(void)
{
return &i;
}


B.c:
====
int *a = foo();


In the above code, is it valid to make a call "int *a = foo();" in B.c?
What happens when I try it?
 
B

Bill Pursell

C_Programmer said:
Question#1:
What is the default value for any Static Global variable?
http://c-faq.com/decl/initval.html


Question#2:
Can I access a Static Global variable from a different file as given in
the following example?

Example:
A.c:
static int i;
int * foo(void){return &i;}

B.c:
int *a = foo();

B.c should have a prototype for foo, and it should
be declared as extern, and you'll need to make sure
the definition of foo is available at link time.
In the above code, is it valid to make a call "int *a = foo();" in B.c?
What happens when I try it?

I know what happens when I try it. If you try it, then you
will know what happens when you try it. Experimentation
is the key to learning. Contrary to certain claims, you
don't actually need to worry about demons flying out
of your nose if you do something wrong.
 
W

Walter Roberson

Question#1:
==========
==========
What is the default value for any Static Global variable?

C89 3.5.7
If an object that has static storage duration is not initialized
explicitly, it is initialized implicitly as if every member
that has arithmetic type were assigned 0 and every member
that has pointer type were assigned a null pointer constant.

Question#2:
==========
==========
Can I access a Static Global variable from a different file as given in
the following example?

static int i;

int * foo(void)
{
return &i;
}
B.c:
====
int *a = foo();
In the above code, is it valid to make a call "int *a = foo();" in B.c?

Yes, provided you have a proper prototype in scope, and the above
code fragment was inside a function.

What happens when I try it?

The address of A's i will be stored in B's a .

C pointers work by storage addresses, not by name, so if in B you had

int i = 42;
int *a = foo();

then the i from B would *not* interfere in any way with the i from A.
 
C

Chris Torek

I know what happens when I try it. If you try it, then you
will know what happens when you try it. Experimentation
is the key to learning. Contrary to certain claims, you
don't actually need to worry about demons flying out
of your nose if you do something wrong.

Experimentation is all well and good, but it has its limits. For
instance, what happens if you experimentally try driving through
red stop-lights? (It often works, but I do not recommend it.) [%]

(Bill Pursell already gave the correct answer to the first
question, and someone else already gave the correct answer to
the second, so I have snipped those.)

[% There is a joke about someone out in the country driving around,
zooming through all the red lights. His passenger, a city girl,
is understandably terrified. He keeps telling her: "Don't worry,
my brother taught me to drive!" Then he comes across a green
light, and screeches to a stop.

"What are you doing?" she asks. "You went screaming through all
the red lights, why are you stopping at this green one?"

"Are you kidding? My brother might be out there driving!"]

Experimentation will show you what *your* compiler does, at least
with whatever settings you chose. Any change to anything, however,
might change the result, if the effect of whatever construct you
is undefined:

int *p = (int *)0x40002078;
*p = 42;

This code fragment "works" on some machines, but probably not on
yours.
 
C

C_Programmer

Thanks for all the posts. I ran these on my system.
I was kind of expecting compiler errors or warnings where I used foo()
in B.c,
but as it explained in Walter Roberson's response it did not.

Walter: Could you please explain me what do you mean by "C pointers
work by storage addresses, not by name".

Chris Torek: I tried the lines that u posted. It did not give any
compilation warnings, but when I ran
it gave me segmentation fault. is it because the address location
"0x40002078" may not be valid for my
system?

Thanks.

Chris said:
I know what happens when I try it. If you try it, then you
will know what happens when you try it. Experimentation
is the key to learning. Contrary to certain claims, you
don't actually need to worry about demons flying out
of your nose if you do something wrong.

Experimentation is all well and good, but it has its limits. For
instance, what happens if you experimentally try driving through
red stop-lights? (It often works, but I do not recommend it.) [%]

(Bill Pursell already gave the correct answer to the first
question, and someone else already gave the correct answer to
the second, so I have snipped those.)

[% There is a joke about someone out in the country driving around,
zooming through all the red lights. His passenger, a city girl,
is understandably terrified. He keeps telling her: "Don't worry,
my brother taught me to drive!" Then he comes across a green
light, and screeches to a stop.

"What are you doing?" she asks. "You went screaming through all
the red lights, why are you stopping at this green one?"

"Are you kidding? My brother might be out there driving!"]

Experimentation will show you what *your* compiler does, at least
with whatever settings you chose. Any change to anything, however,
might change the result, if the effect of whatever construct you
is undefined:

int *p = (int *)0x40002078;
*p = 42;

This code fragment "works" on some machines, but probably not on
yours.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
 

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
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top