extern struct variable

B

Bangalore

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:
 
S

Srini

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
 
R

Rolf Magnus

Bangalore said:
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.
 
M

maadhuu

hello, what you could do is declare a pointer of type struct and then use
that.
 
H

Howard

maadhuu said:
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
 
S

Srini

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top