typedef struct

Õ

ÕÅÔ´ Î÷±±¹¤Òµ´óѧ

On 5/31/2012 3:10 PM, Kulin wrote:
[...]
My desire is simply to avoid having to create pointers as struct * inside
the structure. Other than that I would tend to agree.

typedef struct A B; // "incomplete type" declaration

can i understand in this way:the ambiguity of struct A is B ?

I don't know what you mean by "the ambiguity of." The line
actually says two things:

1) A type named `struct A' exists. The type is "incomplete,"
meaning that there is no information about the number of bytes a
`struct A' requires or what its member elements are. Perhaps the
code will provide such details later to "complete" the type, but
no details are available yet.

2) `B' is an alias for `struct A'. `B' and `struct A' can
be used interchangeably; both refer to the exact same type. Since
`struct A' is an incomplete type, `B' is also an incomplete type
(because it is, in fact, the same type).

These lines "complete" the `struct A' type by describing the
struct elements. Since `struct A' is now complete, its alias `B'
is also complete: As before, `struct A' and `B' mean the same thing.

--
Eric Sosman
(e-mail address removed)


On 5/31/2012 3:10 PM, Kulin wrote:
[...]
My desire is simply to avoid having to create pointers as struct * inside
the structure. Other than that I would tend to agree.

typedef struct A B; // "incomplete type" declaration

can i understand in this way:the ambiguity of struct A is B ?

I don't know what you mean by "the ambiguity of." The line
actually says two things:

1) A type named `struct A' exists. The type is "incomplete,"
meaning that there is no information about the number of bytes a
`struct A' requires or what its member elements are. Perhaps the
code will provide such details later to "complete" the type, but
no details are available yet.

2) `B' is an alias for `struct A'. `B' and `struct A' can
be used interchangeably; both refer to the exact same type. Since
`struct A' is an incomplete type, `B' is also an incomplete type
(because it is, in fact, the same type).

These lines "complete" the `struct A' type by describing the
struct elements. Since `struct A' is now complete, its alias `B'
is also complete: As before, `struct A' and `B' mean the same thing.

thanks a lot,sorry for my awful english,since i'm a Chinese.
can you exlain why the code below are forbidden ?
typedef struct A {
A *p1; /* not allowed */
B *p2; /* not allowed */
} B;

This gives compilation errors.
 
E

Eric Sosman

[...]
thanks a lot,sorry for my awful english,since i'm a Chinese.
can you exlain why the code below are forbidden ?
typedef struct A {
A *p1; /* not allowed */

`struct A' is the name of a type, but `A' alone is not. The compiler
has no idea at all what `p1' should point to.
B *p2; /* not allowed */

`B' means nothing (yet), because `B' has not been declared. The
compiler has no idea what `p2' should point to.
} B;

This gives compilation errors.

Yes.
 
Õ

ÕÅÔ´ Î÷±±¹¤Òµ´óѧ

[...]
thanks a lot,sorry for my awful english,since i'm a Chinese.
can you exlain why the code below are forbidden ?
typedef struct A {
A *p1; /* not allowed */

`struct A' is the name of a type, but `A' alone is not. The compiler
has no idea at all what `p1' should point to.
B *p2; /* not allowed */

`B' means nothing (yet), because `B' has not been declared. The
compiler has no idea what `p2' should point to.
} B;

This gives compilation errors.

Yes.

Thank you for your patience .I'm new to C.
Can you give me some advice?
blog or maillist?
Thank you,this is first time i contact with others in google group.I like the atmosphere here.
 
E

Eric Sosman

[...]
Thank you for your patience .I'm new to C.
Can you give me some advice?
blog or maillist?
Thank you,this is first time i contact with others in google group.I like the atmosphere here.

This is not "Google group;" this is Usenet. Usenet has been around since
Google's founders were three years old. Their company does many things well,
but the "Google Groups" interface to Usenet is not among their successes.

If you are new to C, get yourself a good book. "The C Programming
Language"
by Kernighan and Ritchie is a good one if you already have experience
with other
programming languages. (It will teach you C, but will not teach you how
to write
programs.) Other books exist, some good and some not. I do not know whether
there are good C books in Chinese.

Another useful source is the comp.lang.c Frequently Asked Questions
(FAQ)
page at <http://www.c-faq.com/>. The coverage is somewhat spotty since
it tries
to address question that arise frequently, and some answers are a little
dated.
Nonetheless, it's an excellent place to look before posting a question.

There's also a Usenet group called "alt.comp.lang.learn.c-c++". I know
nothing about its quality or usefulness, but the title looks helpful.
There might
be some Chinese-language C groups or forums; I don't know.
 
Z

Zhang Yuan

[...]
Thank you for your patience .I'm new to C.
Can you give me some advice?
blog or maillist?
Thank you,this is first time i contact with others in google group.I like the atmosphere here.

This is not "Google group;" this is Usenet. Usenet has been around since
Google's founders were three years old. Their company does many things well,
but the "Google Groups" interface to Usenet is not among their successes.

If you are new to C, get yourself a good book. "The C Programming
Language"
by Kernighan and Ritchie is a good one if you already have experience
with other
programming languages. (It will teach you C, but will not teach you how
to write
programs.) Other books exist, some good and some not. I do not know whether
there are good C books in Chinese.

Another useful source is the comp.lang.c Frequently Asked Questions
(FAQ)
page at <http://www.c-faq.com/>. The coverage is somewhat spotty since
it tries
to address question that arise frequently, and some answers are a little
dated.
Nonetheless, it's an excellent place to look before posting a question.

There's also a Usenet group called "alt.comp.lang.learn.c-c++". I know
nothing about its quality or usefulness, but the title looks helpful.
There might
be some Chinese-language C groups or forums; I don't know.

Thank you.This Usenet is more helpful than those in china.
Students in china usually read books translated from English .
I'm reading c primer plus in Chinese.

Thank you!
 
Z

Zhang Yuan

[...]
Thank you for your patience .I'm new to C.
Can you give me some advice?
blog or maillist?
Thank you,this is first time i contact with others in google group.I like the atmosphere here.

This is not "Google group;" this is Usenet. Usenet has been around since
Google's founders were three years old. Their company does many things well,
but the "Google Groups" interface to Usenet is not among their successes.

If you are new to C, get yourself a good book. "The C Programming
Language"
by Kernighan and Ritchie is a good one if you already have experience
with other
programming languages. (It will teach you C, but will not teach you how
to write
programs.) Other books exist, some good and some not. I do not know whether
there are good C books in Chinese.

Another useful source is the comp.lang.c Frequently Asked Questions
(FAQ)
page at <http://www.c-faq.com/>. The coverage is somewhat spotty since
it tries
to address question that arise frequently, and some answers are a little
dated.
Nonetheless, it's an excellent place to look before posting a question.

There's also a Usenet group called "alt.comp.lang.learn.c-c++". I know
nothing about its quality or usefulness, but the title looks helpful.
There might
be some Chinese-language C groups or forums; I don't know.

Thank you.
the quality of this usenet is higher than those in china .
chinese students prefer to read american books rather than natives.
i'm reading c primer plus now.
thank you,
It's a pitty that cfaq is blocked in china. - -!
 
J

James Kuyper

can you explain why we can typedef incompletely?
such as some basic implementation on computer for this incompletely typedef?

(sorry for my awful English)

Being able to typedef incompletely is just a side effect of being able
to declare an incomplete struct type:

struct incomplete_type;
struct another_incomplete_type *pointer_to_incomplete_type;

The key point is that for some purposes there's no need to know the
actual type of the struct. This is feasible because the standard
requires that all pointers to structs have the same representation and
alignment requirements, so you don't need to tell the compiler the exact
type, so long as all you're doing is receiving pointers to the type, and
passing them on to other functions that do know what the exact type is.
 
J

James Kuyper

On 06/08/2012 02:22 PM, Zhang Yuan wrote:
....
It's a pitty that cfaq is blocked in china. - -!

What in the world? Why do they find objectionable about the cfaq? I
known that their censors are crazy, I'm just not clear on the particular
brand of craziness that produced that decision.
 
Z

Zhang Yuan

On 06/08/2012 02:22 PM, Zhang Yuan wrote:
...

What in the world? Why do they find objectionable about the cfaq? I
known that their censors are crazy, I'm just not clear on the particular
brand of craziness that produced that decision.

Me neither.use some software I can go to cfaq.
This is not difficult for students in china .
pitty.sometimes ,i even can't go to google group.
 
Z

Zhang Yuan

Being able to typedef incompletely is just a side effect of being able
to declare an incomplete struct type:

struct incomplete_type;
struct another_incomplete_type *pointer_to_incomplete_type;

The key point is that for some purposes there's no need to know the
actual type of the struct. This is feasible because the standard
requires that all pointers to structs have the same representation and
alignment requirements, so you don't need to tell the compiler the exact
type, so long as all you're doing is receiving pointers to the type, and
passing them on to other functions that do know what the exact type is.

thank you .

Being able to typedef incompletely is just a side effect of being able
to declare an incomplete struct type:

struct incomplete_type;
struct another_incomplete_type *pointer_to_incomplete_type;

The key point is that for some purposes there's no need to know the
actual type of the struct. This is feasible because the standard
requires that all pointers to structs have the same representation and
alignment requirements, so you don't need to tell the compiler the exact
type, so long as all you're doing is receiving pointers to the type, and
passing them on to other functions that do know what the exact type is.




Being able to typedef incompletely is just a side effect of being able
to declare an incomplete struct type:

struct incomplete_type;
struct another_incomplete_type *pointer_to_incomplete_type;

The key point is that for some purposes there's no need to know the
actual type of the struct. This is feasible because the standard
requires that all pointers to structs have the same representation and
alignment requirements, so you don't need to tell the compiler the exact
type, so long as all you're doing is receiving pointers to the type, and
passing them on to other functions that do know what the exact type is.




Being able to typedef incompletely is just a side effect of being able
to declare an incomplete struct type:

struct incomplete_type;
struct another_incomplete_type *pointer_to_incomplete_type;

The key point is that for some purposes there's no need to know the
actual type of the struct. This is feasible because the standard
requires that all pointers to structs have the same representation and
alignment requirements, so you don't need to tell the compiler the exact
type, so long as all you're doing is receiving pointers to the type, and
passing them on to other functions that do know what the exact type is.




Being able to typedef incompletely is just a side effect of being able
to declare an incomplete struct type:

struct incomplete_type;
struct another_incomplete_type *pointer_to_incomplete_type;

The key point is that for some purposes there's no need to know the
actual type of the struct. This is feasible because the standard
requires that all pointers to structs have the same representation and
alignment requirements, so you don't need to tell the compiler the exact
type, so long as all you're doing is receiving pointers to the type, and
passing them on to other functions that do know what the exact type is.

thank you.i know a lot .
what do the same representation and alignment requirements mean?
 
Z

Zhang Yuan

Being able to typedef incompletely is just a side effect of being able
to declare an incomplete struct type:

struct incomplete_type;
struct another_incomplete_type *pointer_to_incomplete_type;

The key point is that for some purposes there's no need to know the
actual type of the struct. This is feasible because the standard
requires that all pointers to structs have the same representation and
alignment requirements, so you don't need to tell the compiler the exact
type, so long as all you're doing is receiving pointers to the type, and
passing them on to other functions that do know what the exact type is.

thank you,i know a lot.
can you explain below for me?
the same representation and alignment requirements
 
J

James Kuyper

thank you.i know a lot .
what do the same representation and alignment requirements mean?

Every C object is stored in a series of bits; except for bit-fields,
those bits usually fill up entire bytes. Every C type has an
implementation-dependent mapping which connects bit patterns to values.
That mapping is called the representation used by that type. Unsigned
integer types have a simple binary representation. C signed integers
have one of three types of representation: 1's complement, 2's
complement, and sign-magnitude. They all store positive numbers the same
way as the corresponding unsigned type, they differ only in the way they
represent negative values. The C standard imposes no requirements on the
representation of floating point types, but does say that if
__STDC_IEC_559__ is pre-defined by the implementation, the
implementation of floating point types should conform to the
requirements of annex F, which details the ways in which they are
required to conform to the requirements of IEEE/IEC 60559.

Byte ordering (bigendian, littleendian, middleendian, etc.) is also part
of the representation. The standard imposes no requirements on byte
ordering. The C standard also says nothing about how pointers are
represented. That's entirely up to the implementation. However, it does
require that, in some cases, pointers to different types must be
represented the same way.

The alignment of a type is a requirement that objects of that type be
allocated only at particular types of addresses. For instance, if an
object may only be allocated at even addresses, it has an alignment
requirement of 2. If it can only be allocated at addresses which are
integer multiples of 4, it has an alignment requirement of 4.
 
Z

Zhang Yuan

Every C object is stored in a series of bits; except for bit-fields,
those bits usually fill up entire bytes. Every C type has an
implementation-dependent mapping which connects bit patterns to values.
That mapping is called the representation used by that type. Unsigned
integer types have a simple binary representation. C signed integers
have one of three types of representation: 1's complement, 2's
complement, and sign-magnitude. They all store positive numbers the same
way as the corresponding unsigned type, they differ only in the way they
represent negative values. The C standard imposes no requirements on the
representation of floating point types, but does say that if
__STDC_IEC_559__ is pre-defined by the implementation, the
implementation of floating point types should conform to the
requirements of annex F, which details the ways in which they are
required to conform to the requirements of IEEE/IEC 60559.

Byte ordering (bigendian, littleendian, middleendian, etc.) is also part
of the representation. The standard imposes no requirements on byte
ordering. The C standard also says nothing about how pointers are
represented. That's entirely up to the implementation. However, it does
require that, in some cases, pointers to different types must be
represented the same way.

The alignment of a type is a requirement that objects of that type be
allocated only at particular types of addresses. For instance, if an
object may only be allocated at even addresses, it has an alignment
requirement of 2. If it can only be allocated at addresses which are
integer multiples of 4, it has an alignment requirement of 4.

thank you!very useful for me!
your explanation is very explicitï¼
 
K

Keith Thompson

Zhang Yuan said:
Another useful source is the comp.lang.c Frequently Asked
Questions (FAQ) page at <http://www.c-faq.com/>. The coverage is
somewhat spotty since it tries to address question that arise
frequently, and some answers are a little dated. Nonetheless, it's
an excellent place to look before posting a question.
[...]
It's a pitty that cfaq is blocked in china. - -!

That's surprising.

The author of the FAQ is Steve Summit <[email protected]>. He might
be able to offer you some advice about how to obtain a copy.
(It's up to you to decide whether that's a good idea.)
 
Z

Zhang Yuan

Zhang Yuan said:
Another useful source is the comp.lang.c Frequently Asked
Questions (FAQ) page at <http://www.c-faq.com/>. The coverage is
somewhat spotty since it tries to address question that arise
frequently, and some answers are a little dated. Nonetheless, it's
an excellent place to look before posting a question.
[...]
It's a pitty that cfaq is blocked in china. - -!

That's surprising.

The author of the FAQ is Steve Summit <[email protected]>. He might
be able to offer you some advice about how to obtain a copy.
(It's up to you to decide whether that's a good idea.)

--
Keith Thompson (The_Other_Keith) (e-mail address removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

I can connect to c-faq temporally now.
I found copy in ftp://ftp.eskimo.com/u/s/scs/C-faq
thank you!
 
K

Keith Thompson

Robert Miles said:
Looks like someone needs to answer "Pushing you out a rather high window
is something. Therefore, we must do that too."

Any particular reason?
 
K

Keith Thompson

Robert Miles said:
Whoever that quote came from appears to need to think some more, and
this answer should help start that thinking.

Perhaps you missed the fact that the quote is intended to be humorous.
 
J

James Kuyper

Perhaps you missed the fact that the quote is intended to be humorous.

If was clearly intended to be humorous by the real-world author.
However, was it intended to be humorous by the fictional character who
spoke that line? Having seen only a little of that show, I had assumed
that it was an expression of bureaucratic incompetence so extreme that
"Pushing you out a rather high window" would have led to a significant
improvement in government efficiency. There did seem to be several
characters on that show who could plausibly have spoken such a line
without humorous intent.
 

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

Forum statistics

Threads
474,079
Messages
2,570,573
Members
47,205
Latest member
ElwoodDurh

Latest Threads

Top