473,396 Members | 1,917 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,396 software developers and data experts.

using extern correctly in C

Hi

I know this is a noob question, but i just wanted to make sure that I what i'm doing is correct.

I've used the extern keyword to declare the same variables in different .c files, so that the compiler understands that it is supposed to be the same variable, not 2 different variables that i've accidently given the same name.

extern NODE TreeNodes[SIZE]; is included in several of my .c files that all use TreeNodes[x] as the same variable. is this right?

thanks

charlie
Feb 15 '07 #1
3 2213
AdrianH
1,251 Expert 1GB
Hi

I know this is a noob question, but i just wanted to make sure that I what i'm doing is correct.

I've used the extern keyword to declare the same variables in different .c files, so that the compiler understands that it is supposed to be the same variable, not 2 different variables that i've accidently given the same name.

extern NODE TreeNodes[SIZE]; is included in several of my .c files that all use TreeNodes[x] as the same variable. is this right?

thanks

charlie
Yes... sort of.

What you are doing will do what you are asking. However, for maintainability reasons, it is better to declare this in one header file and include that header file. Even then, global variables are not easily maintainable in themselves as they can be modified anywhere making debugging more difficult, and making documentation even more necessary than normal.

What I have done in the past in C is to declare the functions related to the 'global object' in a header file, and define them in the source file. The 'global object' would then be declared as static in the source file near the beginning.

In this way, all code related to the 'global object' (which is no longer accessible out side the source file, so is actually a file scoped object) is located in one spot, implying that they are users of that object.

Alternatively, in C++ if there is only to be one instance of this object, you can use the standard Singleton Pattern, which is a design pattern which enforces only one can be made. This can be done as follows:

Header file:
Expand|Select|Wrap|Line Numbers
  1. class foo
  2. {
  3. private:
  4.   foo();  // constructor is only available by those who are foo objects.
  5.   static foo singleInstance; // This is only one instance.
  6.   static foo arrayInstance[100]; // There is only one instance of this array.
  7.  
  8. public:
  9.   static foo& getSingleInstance();
  10.   static foo& getElement(int index);
  11. };
  12.  
Source file:
Expand|Select|Wrap|Line Numbers
  1. #include "foo.h"
  2.  
  3. // These static variables have access to the private
  4. // constructor as they are members of class foo.
  5.  
  6. // Execute the default constructor on this instance
  7. foo foo::singleInstance();
  8.  
  9. // Explicitly stating to use the default constructor on first two elements,
  10. // the rest will implicitly use the default constructor.
  11. foo foo::arrayInstance={ foo(), foo() };
  12.  
If you don't want to pass out the objects via getSingleInstance() or getArrayInstance(), then you could (if only having a single instance) create static functions that would assume that it was meant for the static instance. If only using an array instance, then you could have static functions that would take an index and work on the instance that resides at that index.

What it comes down to is maintainability. However, if you are making a one shot programme where you hope you don’t have to maintain it or debug it later, maybe this isn’t an issue for you.

Does what I am saying make sense? I hope this helps.


Adrian
Feb 15 '07 #2
Thats great, I understand it fully

Thanks

Charlie
Feb 16 '07 #3
AdrianH
1,251 Expert 1GB
Thats great, I understand it fully

Thanks

Charlie
No prob. Glad to help.


Adrian
Feb 16 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Saulius | last post by:
Hello, I am experiencing a strange problem using file streams in a DLL library. I am using Borland C++ Builder 5.0 Professional and I am trying to simply read a file using ifstream inside a DLL....
4
by: usr2003 | last post by:
I wrote the following test program to test the linkage directives extern "C": #include <stdio.h> extern "C" {
22
by: Ian | last post by:
The title says it all. I can see the case where a function is to be called directly from C, the name mangling will stuff this up. But I can't see a reason why a template function can't be...
4
by: Oliver | last post by:
Hello ! I am trying to get the unrar.dll working in C#... it seems that I correctly imported the functions as the first 2 function work without problem (RAROpenArchive & RARGetDLLVersion)......
9
by: Guy | last post by:
I have extended the datetimepicker control to incorporate a ReadOnly property. I have used the new keyword to implement my own version of the value property, so that if readonly == true then it...
0
by: Jim dunn | last post by:
HI I am having problems with C# with regards to its compatibility with win32 API methods, I am trying to read from a windows CE comm port using C# and imported methods from coredll.dll, it seems...
7
by: Matthias Matker | last post by:
Hello! I am sure you can help me with tis question: I am writing a programme which uses severall classes, but I have a "shared" file which contains definitions used by several classes. ----...
5
by: Rahul | last post by:
Hi Everyone, I wanted to know if the following declaration is valid? extern int i = 10; #include <cstdio> int main() {
7
by: blangela | last post by:
I am currently taking a course and one of the example programs showed a function prototype something like: extern void func(); I asked why the "extern" was necessary. Someone in the class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.