By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,486 Members | 1,716 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,486 IT Pros & Developers. It's quick & easy.

extern struct variable

P: n/a
Hi,
Like fundamental data types (int,char,float,...),why can't we are
not able to make userdefined datatypes as extern.

if "file1.C" conatains
struct a
{
int x;
} b = { 3 };

and in "file2.C" if I want to access b.x, by making
extern struct a b;
the compiler will flash error.

Thanks
Bangalore. :lol:

Aug 2 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
> Hi,
Like fundamental data types (int,char,float,...),why can't we are
not able to make userdefined datatypes as extern.

if "file1.C" conatains
struct a
{
int x;
} b = { 3 };

and in "file2.C" if I want to access b.x, by making
extern struct a b;
the compiler will flash error.


The problem here is that in file2.c, the compiler does not know what
"struct a" is. For this, it must be able to see its definition. Try
this...

// file.h
struct A
{
int x;
};

// file1.cpp
#include "file.h"

A obj = {3}; // struct keyword not required here! its C++

// file2.cpp
#include "file.h"

extern A obj; // defined elsewhere

Now you can compile file1.cpp and file2.cpp seperately and link them
together.

HTH
Regards,
Srini

Aug 2 '05 #2

P: n/a
Bangalore wrote:
Hi,
Like fundamental data types (int,char,float,...),why can't we are
not able to make userdefined datatypes as extern.

if "file1.C" conatains
struct a
{
int x;
} b = { 3 };

and in "file2.C" if I want to access b.x, by making
extern struct a b;
the compiler will flash error.


That's because the compiler only know that there is a struct called 'a'. It
doesn't know anything else about it, like that it has a member called 'x'.
So the compiler cannot use it without seeing the whole definition of the
struct.

Aug 2 '05 #3

P: n/a
hello, what you could do is declare a pointer of type struct and then use
that.

Aug 2 '05 #4

P: n/a

"maadhuu" <ma************@yahoo.com> wrote in message
news:b6******************************@localhost.ta lkaboutprogramming.com...
hello, what you could do is declare a pointer of type struct and then use
that.


Eh? What's a "pointer of type struct"? And how would a pointer solve the
problem that the compiler can't see his struct definition (since the struct
definition is in a file which is not #include'd)?

-Howard
Aug 2 '05 #5

P: n/a
> Hi,
Like fundamental data types (int,char,float,...),why can't we are
not able to make userdefined datatypes as extern.

if "file1.C" conatains
struct a
{
int x;
} b = { 3 };

and in "file2.C" if I want to access b.x, by making
extern struct a b;
the compiler will flash error.

The problem here is that in file2.c, the compiler does not know what
"struct a" is. For this, it must be able to see its definition. Try
this...

// file.h
struct A
{
int x;
};

// file1.cpp
#include "file.h"

A obj = {3}; // struct keyword not required here! its C++

// file2.cpp
#include "file.h"

extern A obj; // defined elsewhere

Now you can compile file1.cpp and file2.cpp seperately and link them
together.

HTH
Regards,
Srini

Sep 6 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.