473,394 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

forward declaration

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
);

Oct 2 '07 #1
7 5036
On Oct 3, 5:23 am, RedLars <Liverpool1...@gmail.comwrote:
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.

Oct 2 '07 #2
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

Oct 2 '07 #3
On 3 Okt, 01:40, boroph...@gmail.com wrote:
On Oct 3, 5:23 am, RedLars <Liverpool1...@gmail.comwrote:
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 -

Oct 3 '07 #4
On Oct 3, 3:22 pm, RedLars <Liverpool1...@gmail.comwrote:
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.

Oct 4 '07 #5
On 4 Okt, 06:03, boroph...@gmail.com wrote:
On Oct 3, 3:22 pm, RedLars <Liverpool1...@gmail.comwrote:
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.

Oct 7 '07 #6
RedLars <Li***********@gmail.comwrites:
[...]
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) ks***@mib.org <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"
Oct 7 '07 #7
On Sun, 07 Oct 2007 13:17:22 -0700, Keith Thompson <ks***@mib.org>
wrote:
RedLars <Li***********@gmail.comwrites:
<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
Oct 22 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: mjm | last post by:
Folks, Please help me with the following problems: ******************************************** 1. I have a class template template<class Base> class Matrix : public Base { /* .... */ }
6
by: Steven T. Hatton | last post by:
Should I be able to forward declare something from a namespace different from the current one? For example the following code compiles: //testdriver.hpp #ifndef TESTDRIVER_HPP #define...
6
by: Markus Dehmann | last post by:
I have a circular dependency between two classes. The FAQ hint about a forward declaration does not help in my case ( How can I create two classes that both know about each other?) Error:...
2
by: Plok Plokowitsch | last post by:
After over a decade of programming in C++ I seem to have missed some substantial point (mental note: am I getting too old for this?). A little bit of help would be *very* appreciated. I'm trying...
2
by: blueblueblue2005 | last post by:
Hi, there was a post several days ago about using forward class declaration to solve the circular including issue, today, one question suddenly came into mind: which class should the forward class...
3
by: Michael Sgier | last post by:
Hello with the original code below I get the error: "forward declaration of `struct CPlayer'" class CPlayer; // what does this do? Instantiate the class CPlayer? // what's a forward...
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
6
by: Hunk | last post by:
Hi I have a question on usage of forward declarations of templates. While searching through this group , people suggested using forward declarations and typedefs for templates as // in...
4
by: Steve | last post by:
Hi, I always though to return an instance of a class by value, it had to be defined - i.e. forward declaration isn't good enough? Consider the following code snippet: class RGBA; class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.