Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > external struct declaration

Reply
Thread Tools

external struct declaration

 
 
anonymous
Guest
Posts: n/a
 
      05-16-2004
I have a Lex file containing definitions of 2 structures like:

%{
struct a {...};
struct b
{
struct a i;
...
};
struct a x[10];
struct b y[10];
....
%}

In main program I have external declarations

/* main.c */

extern struct a;
extern struct b;
extern struct a x[];
extern struct b y[];

When I try to compile and run I am getting

'Warning:Useless keyword or type name in external declaration'
and
'Invalid use of undefined type : struct a'

Is there anything wrong in the external declarations
of the structure?
 
Reply With Quote
 
 
 
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      05-16-2004
anonymous <(E-Mail Removed)> wrote:
> I have a Lex file containing definitions of 2 structures like:


> %{


[Hint for those not used to lex: the '%{' starts a section of
C code which goes unchanged into the C source file created from
the lex file, so the question isn't really OT.]

> struct a {...};


This makes a new structure type 'a' known within that (lex) file
(but only within that file, nowhere else).

> struct b
> {
> struct a i;
> ...
> };


And that of the new structure type 'b'.

> struct a x[10];
> struct b y[10];


And here you define two arrays of structures of type 'a' and 'b'.
Everything fine so far.

> In main program I have external declarations


> /* main.c */


> extern struct a;


You can use 'extern' only when there's a variable or function name
following it. What you write here is basically like having a line like

extern int;

which simply makes doesn't make sense. That explains the first warning

> 'Warning:Useless keyword or type name in external declaration'


Even worse, when the compiler reads that file it has no idea at all
what 'struct a' is or means. It doesn't know that 'struct a' has been
(or may only will be) declared in a different source file. What you
need to do is either declare again here what 'struct a' means - but
that's a rather bad idea, because it's too easy to have the decla-
rations get out of sync - or to create an include file, into which
you put what 'struct a' is, and then include it from every file that
needs to know what 'struct a' means. If you do that the second warning

> 'Invalid use of undefined type : struct a'


for the line

> extern struct a x[];


will disappear.
Regards, Jens
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
 
 
 
anonymous
Guest
Posts: n/a
 
      05-16-2004
[...]
> Even worse, when the compiler reads that file it has no idea at all
> what 'struct a' is or means. It doesn't know that 'struct a' has been
> (or may only will be) declared in a different source file.

[...]

But when I create the executable, both the .o files are used:

gcc -o lex.yy.o main.o

so, the definitions must be visible - right?
 
Reply With Quote
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      05-16-2004
anonymous <(E-Mail Removed)> wrote:
> [...]
>> Even worse, when the compiler reads that file it has no idea at all
>> what 'struct a' is or means. It doesn't know that 'struct a' has been
>> (or may only will be) declared in a different source file.

> [...]


> But when I create the executable, both the .o files are used:


> gcc -o lex.yy.o main.o


> so, the definitions must be visible - right?


But then you're already linking. But what you got was an error
message from the compiler. And the compiler only deals with
single files at a time (if you don't count included files, but
they simple are more or less pasted in the source). So, when
the compiler sees your yy.c file (or however it's called) it
finds the definition of the structure and creates an .o file from
it without problems. But when it gets to main.c (the one where you
also use the structure), it has forgotten everything it did see in
the other file (typically, it's a completely new invocation of the
compiler) and so it has no idea what that structure is supposed to
be and complains, and you don't even get a main.o file. The easiest
way to make the type "struct a" visible to both files is to put the
definition into an additional include file that then gets included
from both files.
Regards, Jens
--
\ Jens Thoms Toerring ___ (E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
Michael Wojcik
Guest
Posts: n/a
 
      05-18-2004

In article <(E-Mail Removed)>, (E-Mail Removed)-berlin.de writes:
> anonymous <(E-Mail Removed)> wrote:
> > /* main.c */
> > extern struct a;

>
> You can use 'extern' only when there's a variable or function name
> following it. What you write here is basically like having a line like
>
> extern int;
>
> which simply makes doesn't make sense. That explains the first warning
>
> > 'Warning:Useless keyword or type name in external declaration'


Good so far.

> Even worse, when the compiler reads that file it has no idea at all
> what 'struct a' is or means.


Incomplete struct declarations are fine in C, provided you don't try
to use them as complete types, and in fact they're useful for
declaring opaque types in at least one context - function prototypes.

If main.c doesn't need to complete structs a and b (if it doesn't
need to do anything with their members, and doesn't need to take
their size, eg using the sizeof operator), then:

struct a;
struct b;

would be fine in main.c, to introduce the names "a" and "b" to the
struct namespace.

The only use I can think of offhand for this "bare" struct tag
declaration is before a function prototype which uses a pointer to
the struct, as in

struct a;
extern void foo(struct a *);

because there a previously-unseen struct tag (if you omitted the
"struct a;" line) will declare the structure only in prototype scope,
which is basically useless.

> It doesn't know that 'struct a' has been
> (or may only will be) declared in a different source file.


Which is fine, since it doesn't need to know whether any other TU
("source file") has declared struct a. That's irrelevant to the
behavior of main.c.

In this case, the OP probably wants to use struct a as a complete
type - if main.c is going to operate on that that array of struct a,
it needs to be complete - but there are APIs that use incomplete
structure definitions to create and pass opaque types safely.

--
Michael Wojcik http://www.velocityreviews.com/forums/(E-Mail Removed)

Advertising Copy in a Second Language Dept.:
Tapestry of the encounting and the farewell, the birth and the death.
You can hear the human being's song running through the 100 years.
-- Squaresoft
 
Reply With Quote
 
Peter Shaggy Haywood
Guest
Posts: n/a
 
      05-19-2004
Groovy hepcat (E-Mail Removed)-berlin.de was jivin' on 16 May
2004 10:51:23 GMT in comp.lang.c.
Re: external struct declaration's a cool scene! Dig it!

>anonymous <(E-Mail Removed)> wrote:
>
>> extern struct a;

>
>You can use 'extern' only when there's a variable or function name
>following it. What you write here is basically like having a line like
>
>extern int;
>
>which simply makes doesn't make sense. That explains the first warning


Actually, as the compiler (probably) sees it, the errant line
declares an external object of type struct. Hence this warning:

>> 'Warning:Useless keyword or type name in external declaration'


"struct" (without a tag) is indeed a useless keyword or type name.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
Reply With Quote
 
leopascual leopascual is offline
Junior Member
Join Date: May 2008
Posts: 1
 
      05-11-2008
The problem is you need to specify all the struct declaration where u are trying to declare it as extern, this is because you know it's a type construct by you so it need the struct to make a comparison of where its the original.

example:
FILE 1.c
struct a {
int x;
int y;
}z;

File 2.c
extern struct a {
int x;
int y;
}z;

this compiles but haven't had the chance to prove it. Hope it work!!

Quote:
Originally Posted by anonymous
I have a Lex file containing definitions of 2 structures like:

%{
struct a {...};
struct b
{
struct a i;
...
};
struct a x[10];
struct b y[10];
....
%}

In main program I have external declarations

/* main.c */

extern struct a;
extern struct b;
extern struct a x[];
extern struct b y[];

When I try to compile and run I am getting

'Warning:Useless keyword or type name in external declaration'
and
'Invalid use of undefined type : struct a'

Is there anything wrong in the external declarations
of the structure?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
struct declaration into another struct slocum C++ 3 04-11-2008 09:42 AM
Initialize pointer-to-struct declaration to an unnamed struct? Ehud Shapira C++ 20 06-30-2007 04:10 PM
maxplusII error: a deferred constant declaration without a full declaration is not supported Noah VHDL 5 04-07-2006 02:34 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments