Solved

External variables in structures / unions

Posted on 2003-12-02
7
596 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
[X]
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
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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

636 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