Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > forward declaration

Reply
Thread Tools

forward declaration

 
 
RedLars
Guest
Posts: n/a
 
      10-02-2007
Need some help with a couple of questions. Previously I have seen this
declaration;
struct Student
{
int age;
char name[50];
};

and this definition of an object;
struct Student stud;

However, what does this mean?
struct AnotherStudent;

Another question, how does forward declaration really work? Say I have
these two structs and a function pointer. This wont compile as is. How
can I fix this ?

// File ObjectA.h
typedef struct
{
ObjectB b;
} ObjectA;

// File ObjectB.h
typedef struct
{
func f;
} ObjectB;

int (*func)
(
ObjectA
);

 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      10-02-2007
RedLars wrote:
>
> Need some help with a couple of questions. Previously I have seen
> this declaration;
>
> struct Student {
> int age;
> char name[50];
> };
>
> and this definition of an object;
>
> struct Student stud;
>
> However, what does this mean?
>
> struct AnotherStudent;


In the context, absolutely nothing. You have a type, named "struct
Student" in existance. I suspect the writer meant to define
another object as:

struct Student AnotherStudent;

which would work.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>


--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
 
borophyll@gmail.com
Guest
Posts: n/a
 
      10-02-2007
On Oct 3, 5:23 am, RedLars <(E-Mail Removed)> wrote:
> Need some help with a couple of questions. Previously I have seen this
> declaration;
> struct Student
> {
> int age;
> char name[50];
>
> };
>
> and this definition of an object;
> struct Student stud;
>
> However, what does this mean?
> struct AnotherStudent;


It's a forward declaration

> Another question, how does forward declaration really work? Say I have
> these two structs and a function pointer. This wont compile as is. How
> can I fix this ?
>
> // File ObjectA.h
> typedef struct
> {
> ObjectB b;
>
> } ObjectA;
>
> // File ObjectB.h
> typedef struct
> {
> func f;
>
> } ObjectB;
>
> int (*func)
> (
> ObjectA
> );


Your first question answers this. Just put a forward declaration for
ObjectA at the top of the file ObjectB.h, and vice versa.

 
Reply With Quote
 
RedLars
Guest
Posts: n/a
 
      10-03-2007
On 3 Okt, 01:40, (E-Mail Removed) wrote:
> On Oct 3, 5:23 am, RedLars <(E-Mail Removed)> wrote:
>
> > Need some help with a couple of questions. Previously I have seen this
> > declaration;
> > struct Student
> > {
> > int age;
> > char name[50];

>
> > };

>
> > and this definition of an object;
> > struct Student stud;

>
> > However, what does this mean?
> > struct AnotherStudent;

>
> It's a forward declaration


What does a forward declaration mean? It seems like its 'creating' a
type without defining it's content? How can I later define a forward
declaration's content? This (code below) would not compile, whats the
correct pattern of using forward declaration?

// file abc.h
struct AnotherStudent;

// file xyz.h
struct AnotherStudent { int x, long y, char z };

>
>
>
> > Another question, how does forward declaration really work? Say I have
> > these two structs and a function pointer. This wont compile as is. How
> > can I fix this ?

>
> > // File ObjectA.h
> > typedef struct
> > {
> > ObjectB b;

>
> > } ObjectA;

>
> > // File ObjectB.h
> > typedef struct
> > {
> > func f;

>
> > } ObjectB;

>
> > int (*func)
> > (
> > ObjectA
> > );

>
> Your first question answers this. Just put a forward declaration for
> ObjectA at the top of the file ObjectB.h, and vice versa.- Skjul sitert tekst -
>
> - Vis sitert tekst -



 
Reply With Quote
 
borophyll@gmail.com
Guest
Posts: n/a
 
      10-04-2007
On Oct 3, 3:22 pm, RedLars <(E-Mail Removed)> wrote:

> What does a forward declaration mean? It seems like its 'creating' a
> type without defining it's content?


Correct, it is declaring a type without defining it, so that you can
use the type in other declarations.


How can I later define a forward
> declaration's content?


Just do it. For example:

//objA.h

struct ObjB;

struct ObjA {
struct ObjB * b;
};

//objB.h

struct ObjA;

struct ObjB {
struct ObjA * b;
};

This (code below) would not compile, whats the
> correct pattern of using forward declaration?
>
> // file abc.h
> struct AnotherStudent;
>
> // file xyz.h
> struct AnotherStudent { int x, long y, char z };


It doesn't compile because you are using commas instead of semicolons
to separate
each member.

 
Reply With Quote
 
RedLars
Guest
Posts: n/a
 
      10-07-2007
On 4 Okt, 06:03, (E-Mail Removed) wrote:
> On Oct 3, 3:22 pm, RedLars <(E-Mail Removed)> wrote:
>
> > What does a forward declaration mean? It seems like its 'creating' a
> > type without defining it's content?

>
> Correct, it is declaring a type without defining it, so that you can
> use the type in other declarations.
>
> How can I later define a forward
>
> > declaration's content?

>
> Just do it. For example:
>
> //objA.h
>
> struct ObjB;
>
> struct ObjA {
> struct ObjB * b;
>
> };
>
> //objB.h
>
> struct ObjA;
>
> struct ObjB {
> struct ObjA * b;
>
> };
>
> This (code below) would not compile, whats the
>
> > correct pattern of using forward declaration?

>
> > // file abc.h
> > struct AnotherStudent;

>
> > // file xyz.h
> > struct AnotherStudent { int x, long y, char z };

>
> It doesn't compile because you are using commas instead of semicolons
> to separate
> each member.


Thanks for your reply.

Not sure if this is the right thread for a couple of new questions but
here goes;

>From what I understand this is declaration of a struct;

struct person
{
char * first;
char *last;
int age;
};

And to use this type (allocate memory);
struct person p;

What about this bit of code;
struct
{
char * first;
char *last;
int age;
} person;

Is this equivalent to the first example? If not, what is the
difference?

By adding 'typedef' infront of both these code blocks;
typedef struct person {...};
typedef struct {...} person;
What does that accomplish? Have read in a tutorial that you dont have
to write;
struct person p;
but can instead write;
person p;

Is that the only difference? It doesnt change the scope of the
variable or anything else behind the scenes? Just makes the code
easier to read.

Thanks for any input.



 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-07-2007
RedLars <(E-Mail Removed)> writes:
[...]
> From what I understand this is declaration of a struct;
> struct person
> {
> char * first;
> char *last;
> int age;
> };


The unqualified phrase "a struct" can be ambiguous; it can refer
either to a struct type or to an object (variable) of a struct type.

The above declares a struct type called "struct person".

> And to use this type (allocate memory);
> struct person p;


Right, that declares (and defines) an object of type "struct person".

> What about this bit of code;
> struct
> {
> char * first;
> char *last;
> int age;
> } person;
>
> Is this equivalent to the first example? If not, what is the
> difference?


In your first example, the type is named "struct person" and the
object is named "p". In your second example, the type has no name
(which is bad if you ever want more than one object) and the object is
named "person". Other than that, they're equivalent.

> By adding 'typedef' infront of both these code blocks;
> typedef struct person {...};


That's invalid; what you want is

typedef struct person {...} person;

The first "person" is the struct tag; the second is the typedef name.
You can use the same identifier for both, since a struct tag always
immediately follows the keyword "struct".

> typedef struct {...} person;


Here the struct has no tag (which is ok); its only name is the typedef
name "person".

> What does that accomplish? Have read in a tutorial that you dont have
> to write;
> struct person p;
> but can instead write;
> person p;
>
> Is that the only difference? It doesnt change the scope of the
> variable or anything else behind the scenes? Just makes the code
> easier to read.


A typedef creates an alias for an existing type. That's *all* it
does.

There's a (mostly good-natured) controversy about whether typedefs for
structs are a good idea.

The argument against them is that the type already has a perfectly
good name, and defining an alias doesn't buy you anything; you can
just refer to it everywhere as "struct person" This is more explicit;
the code that uses the type needs to know that it's a struct type
anyway. (The exception is when it's truly an abstract type whose
innards should be hidden from code that uses it; FILE is an example of
this.)

On the other hand, a typedef for a struct does give you a one-word
name for the type, and plenty of programmers feel that this
convenience is worthwhile. <OT>Note that C++ lets you refer to
"struct person" as just "person"; a typedef lets you do the same thing
in C.</OT>

Note that the typedef name isn't available until the end of the
declaration, so you generally have to use the struct tag within the
struct declaration itself:

typedef struct person {
char *name;
struct person *father;
struct person *mother;
} person;
person you;

There are more issues I haven't gone into (whether the typedef name
really should be the same as the struct tag, and if not, how they
should be related; whether the typedef and struct should be declared
together or in separate declarations; and so forth).

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      10-10-2007
On Tue, 02 Oct 2007 12:23:20 -0700, RedLars <(E-Mail Removed)>
wrote:

>Need some help with a couple of questions. Previously I have seen this
>declaration;
>struct Student
>{
> int age;
> char name[50];
>};
>
>and this definition of an object;
>struct Student stud;
>
>However, what does this mean?
>struct AnotherStudent;
>
>Another question, how does forward declaration really work? Say I have
>these two structs and a function pointer. This wont compile as is. How
>can I fix this ?
>
>// File ObjectA.h
>typedef struct
>{
> ObjectB b;
>} ObjectA;
>
>// File ObjectB.h
>typedef struct
>{
> func f;
>} ObjectB;
>
>int (*func)
>(
> ObjectA
>);


It is not possible to have two structure types, each of which has a
member of the other type. This cannot work
struct A (struct B b;} x;
struct B (struct A a;} y;
because the compiler would need to know the size of struct B in order
to define x which it cannot know because struct B has not yet been
defined. Reversing the order of definition simply changes the problem
to knowing the size of struct A.

However, it is possible to have two structure types, each of which has
a member of type pointer to the other type. This is because all
pointers to struct have the same size, alignment, and representation.
This does work
struct B; /*forward definition of incomplete struct type*/
struct A (struct B *b;} x;
struct B (struct A *a;} y; /*complete the struct type*/
because the sizes of x and y no longer depend on the sizes of struct A
and B but only on the sizes of pointers to struct A and B.

A structure is also prohibited from having a member that has function
type but is allowed to have members of pointer to function type.


Remove del for email
 
Reply With Quote
 
David Thompson
Guest
Posts: n/a
 
      10-22-2007
On Sun, 07 Oct 2007 13:17:22 -0700, Keith Thompson <(E-Mail Removed)>
wrote:

> RedLars <(E-Mail Removed)> writes:

<snip>
> > By adding 'typedef' infront of both these code blocks;
> > typedef struct person {...};

>
> That's invalid; what you want is
>
> typedef struct person {...} person;
>

Nit: specifying 'typedef' with no declarator for the/a typedef name is
not what he wanted, and is at best confusing, but it is not a syntax
error or constraint violation and must be 'accepted', which I believe
is agreed to mean translated although not specifically defined.

> The first "person" is the struct tag; the second is the typedef name.
> You can use the same identifier for both, since a struct tag always
> immediately follows the keyword "struct".
>

Nit: after preprocessing, or more specifically translation phase 4.
Although any program that uses a macro to cons* struct+tag might
reasonably be argued to DESERVE to fail. (* well, sort of <G>) And I
can't think offhand of any comment that would really need to go
between them rather than (immediately) before or after.

<snip rest>
- formerly david.thompson1 || achar(64) || worldnet.att.net
 
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
Re-forward declaration of types which were already forward declared qazmlp C++ 1 02-15-2004 07:00 PM
forward declaration? Lem C++ 12 09-22-2003 02:28 PM
Class forward declaration Kevin Grigorenko C++ 6 09-15-2003 03:18 PM
GCC and forward declaration of enum Alexander Grigoriev C++ 11 09-14-2003 07:17 AM
favoring forward class declaration over #include Aaron Anodide C++ 1 08-01-2003 04:51 PM



Advertisments