Solved

External variables in structures / unions

Posted on 2003-12-02
7
586 Views
Last Modified: 2012-05-04
Can I use external variables in
1) structures
2) unions

if No why ???
If yes how ???
0
Comment
Question by:r_bal
7 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 9863536
Hi r_bal,

Yes you can, but perhaps not as you intend.

extern int errno;

This common item tells the compiler and linker that there is a variable called errno that is defined in another module.  The variable is an int which is a complete item that can be reduced to a single address.  It is not a member of a structure.

typedef struct
{
  int SomeValue;
  char Name[20];
  int ErrorNumber;
} MyStruct;

extern int errno;
MyStruct FirstStruct = {0, "Kent", &errno};

Here we've used errno to initialize the value of a structure.  However, you cannot do something like:

typedef struct
{
  extern int errno;
} BadStruct;

In this case the extern clause tells the compiler that a field of the structure is external.  When you stop and think about it, this just can't happen.  Either the entire structure is declared externally (and you CAN define an entire structure as an extern) or none of it is.



Hope this clears things up a bit,
Kent
0
 

Author Comment

by:r_bal
ID: 9864162
So, that means the when you declare a structure, you cannot have any member as extern.

Like you gave an example of

typedef struct
{
  extern int errno;
} BadStruct;
0
 
LVL 1

Expert Comment

by:chikucoder
ID: 9864890
Well Let me put my prespective behind this problem



extern specifier lets know that variable has been defined in another source file as a global,that means memory has been allocated to that variable , By using the extern u r just accessing that variable in another file,suppose if u just include as member of structure, and then u create variable of that structure, then conflict will happen regarding the extern member how to allocating memory for this.

 correct me if i'm worng,
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 2

Expert Comment

by:sin_
ID: 9865675
Here the problem is , the variable that you intend to link is inside a datastructure which needs memory for itself.

See your variable is wrapped inside a struct:

struct abc
{
  extern int a;
  float y;
};

So when you have this declaration, struct understands only an auto variable inside. That's why it complains about illegal strorage class inside.
0
 
LVL 1

Expert Comment

by:chikucoder
ID: 9866273
Hi sin_

>>struct understands only an auto variable inside.

I have doubt on u r comment


#include<sdio.h>
struct abc
{
  int a;
  float y;
};

struct abc A;
void fun();

int main(void)
{
return 0;
}

according the above code fragment structure have been defined and structure variable created ,Even member's of the structure are also global.

So u mean to say structure can't understand the global variable.But above code works fine.

But extern vairables can't be included ,bcoz memory has been already allocated to it,structure is just blue print unless and until structure variable is created memory won't be allocated to any of structure member.

Pls correct me i'm wrong
chiku
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9874193
Kent, you surely mean:
--------8<--------
typedef struct
{
  int SomeValue;
  char Name[20];
  int *ErrorNumber; /* <- Takes an address of an int */
} MyStruct;

extern int errno;
MyStruct FirstStruct = {0, "Kent", &errno};
--------8<--------
...but that's simply initialising a member with the address of something which is extern.

Off topic, but in C++ you appear to get a bit closer, using references:
--------8<--------
#include <iostream>

extern int errno;

struct x {
static const int& e;
};

const int& x::e(errno);

int main()
{
        errno = 45;

        x x1;
        std::cout << "Here is the extern " << x1.e << '\n';

        std::cout << "Or indeed " << x::e << '\n';
}
--------8<--------
...but underneath the covers, it is the same thing really.
0
 
LVL 45

Accepted Solution

by:
Kdo earned 20 total points
ID: 9874282

And much more wordy.   :)


Good catch on the 'int *ErrorNumber'.

Kent
0

Featured Post

Superior storage. Superior surveillance.

WD Purple drives are built for 24/7, always-on, high-definition security systems. With support for up to 8 hard drives and 32 cameras, WD Purple drives are optimized for surveillance.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now