473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Global access of static variable

Can a static variable defined in a module be indirectly accessed in
another module.

File1.c
-------

static int i;
int* ptr;
int main()
{
ptr = &i;
fun();
}

File2.c
--------
extern int* ptr;
fun()
{
printf("value of i = %d",*ptr);

}
Can "i" which is defined in 1.c be accessed in 2.c.

NB:- GCC allows u to do this without any segmentation fault.Can
someone give me the reason.
Nov 14 '05 #1
1 1851
gomathy wrote:
Can a static variable defined in a module be indirectly accessed in
another module.

File1.c
-------

static int i;
int* ptr;
int main()
{
ptr = &i;
fun();
}

File2.c
--------
extern int* ptr;
fun()
{
printf("value of i = %d",*ptr);

}
Can "i" which is defined in 1.c be accessed in 2.c. 'i' cannot be directly accessed, but as you made a (non-static)pointer
to the
same location, you can effectivly manipulate it. NB:- GCC allows u to do this without any segmentation fault.Can
someone give me the reason.

Cause there is nothing wrong.
Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: refer_to_website | last post by:
I realize that if I want a global VARIABLE, I can DIM a session variable in global.asa. I.e. Static oCurrentPerson As New Person() in the Session_Start routine) but attempting to access...
2
by: Lu | last post by:
Hello, I am wondering how to protect a global variable in a header file from external access. So I googled and found: "The keyword 'static' has two different uses, depending on whether it is...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
8
by: Morpheus | last post by:
Hello, Say I have a class with a member... char mId; Whenever an object is created, I want to assign an incrementing character to this member, for instance the first would be A, then B, C,...
2
by: Vinu | last post by:
Hi, I am facing one problem related to global variables in .so file. When ever I access the variable the application is crashing I have a class called services. class services { services(){...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.