• C

External variables in structures / unions

Can I use external variables in
1) structures
2) unions

if No why ???
If yes how ???
r_balAsked:
Who is Participating?
 
Kent OlsenData Warehouse Architect / DBACommented:

And much more wordy.   :)


Good catch on the 'int *ErrorNumber'.

Kent
0
 
Kent OlsenData Warehouse Architect / DBACommented:
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
 
r_balAuthor Commented:
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
Network Scalability - Handle Complex Environments

Monitor your entire network from a single platform. Free 30 Day Trial Now!

 
chikucoderCommented:
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
 
sin_Commented:
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
 
chikucoderCommented:
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
 
rstaveleyCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.