Solved

External variables in structures / unions

Posted on 2003-12-02
7
585 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

708 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

16 Experts available now in Live!

Get 1:1 Help Now