Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

External variables in structures / unions

Posted on 2003-12-02
7
590 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:Kent Olsen
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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:
Kent Olsen earned 20 total points
ID: 9874282

And much more wordy.   :)


Good catch on the 'int *ErrorNumber'.

Kent
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

860 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