Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > typedef struct

Reply
Thread Tools

typedef struct

 
 
张源 西北工业大学
Guest
Posts: n/a
 
      06-08-2012
On Saturday, June 9, 2012 1:20:16 AM UTC+8, Eric Sosman wrote:
> On 6/8/2012 1:02 PM, 张源 西北工业大学 wrote:
> > On Friday, June 1, 2012 3:14:00 AM UTC+8, Eric Sosman wrote:
> >> 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).
>
> >> struct A { // "completes" the type `struct A'
> >> int test;
> >> B *p1;
> >> };

>
> 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
> http://www.velocityreviews.com/forums/(E-Mail Removed)d




On Saturday, June 9, 2012 1:20:16 AM UTC+8, Eric Sosman wrote:
> On 6/8/2012 1:02 PM, 张源 西北工业大学 wrote:
> > On Friday, June 1, 2012 3:14:00 AM UTC+8, Eric Sosman wrote:
> >> 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).
>
> >> struct A { // "completes" the type `struct A'
> >> int test;
> >> B *p1;
> >> };

>
> 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 Removed)d


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.
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      06-08-2012
On 6/8/2012 1:27 PM, 张源 西北工业大学 wrote:
> [...]
> 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.

--
Eric Sosman
(E-Mail Removed)d
 
Reply With Quote
 
 
 
 
张源 西北工业大学
Guest
Posts: n/a
 
      06-08-2012
On Saturday, June 9, 2012 1:32:26 AM UTC+8, Eric Sosman wrote:
> On 6/8/2012 1:27 PM, 锟斤拷源 锟斤拷锟斤拷锟斤拷业锟斤拷学 wrote:
> > [...]
> > 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.
>
> --
> Eric Sosman
> (E-Mail Removed)d


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.




 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      06-08-2012
On 6/8/2012 1:37 PM, 张源 西北工业大学 wrote:
> [...]
> 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.

--
Eric Sosman
(E-Mail Removed)d
 
Reply With Quote
 
Zhang Yuan
Guest
Posts: n/a
 
      06-08-2012
On Saturday, June 9, 2012 1:55:22 AM UTC+8, Eric Sosman wrote:
> On 6/8/2012 1:37 PM, 閿熸枻鎷锋簮 閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷蜂笟閿熸枻鎷峰* wrote:
> > [...]
> > 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.
>
> --
> Eric Sosman
> (E-Mail Removed)d


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!
 
Reply With Quote
 
Zhang Yuan
Guest
Posts: n/a
 
      06-08-2012
On Saturday, June 9, 2012 1:55:22 AM UTC+8, Eric Sosman wrote:
> On 6/8/2012 1:37 PM, 閿熸枻鎷锋簮 閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷蜂笟閿熸枻鎷峰* wrote:
> > [...]
> > 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.
>
> --
> Eric Sosman
> (E-Mail Removed)d


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. - -!
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      06-08-2012
On 06/08/2012 01:20 PM, 寮*婧 瑗垮寳宸ヤ笟澶у* wrote:
> On Friday, June 1, 2012 3:14:32 AM UTC+8, Xavier Roche wrote:
>> Le 31/05/2012 21:10, Kulin a 茅crit :
>>> What if I want to create a pointer to the struct inside the struct?
>>> typedef struct A {
>>> A *p1; /* not allowed */
>>> B *p2; /* not allowed */
>>> } B;

>>
>> Hum, why don't you first define the type incompletely ?
>>
>> typedef struct A A;
>> struct A {
>> A *p1;
>> A *p2;
>> };

>
> 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.

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      06-08-2012
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.

 
Reply With Quote
 
Zhang Yuan
Guest
Posts: n/a
 
      06-08-2012
On Saturday, June 9, 2012 2:35:04 AM UTC+8, James Kuyper wrote:
> 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.


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.
 
Reply With Quote
 
Zhang Yuan
Guest
Posts: n/a
 
      06-08-2012
On Saturday, June 9, 2012 2:31:00 AM UTC+8, James Kuyper wrote:
> On 06/08/2012 01:20 PM, 寮*婧 瑗垮寳宸ヤ笟澶у* wrote:
> > On Friday, June 1, 2012 3:14:32 AM UTC+8, Xavier Roche wrote:
> >> Le 31/05/2012 21:10, Kulin a 茅crit :
> >>> What if I want to create a pointer to the struct inside the struct?
> >>> typedef struct A {
> >>> A *p1; /* not allowed */
> >>> B *p2; /* not allowed */
> >>> } B;
> >>
> >> Hum, why don't you first define the type incompletely ?
> >>
> >> typedef struct A A;
> >> struct A {
> >> A *p1;
> >> A *p2;
> >> };

> >
> > 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.


thank you .

On Saturday, June 9, 2012 2:31:00 AM UTC+8, James Kuyper wrote:
> On 06/08/2012 01:20 PM, 寮*婧 瑗垮寳宸ヤ笟澶у* wrote:
> > On Friday, June 1, 2012 3:14:32 AM UTC+8, Xavier Roche wrote:
> >> Le 31/05/2012 21:10, Kulin a 茅crit :
> >>> What if I want to create a pointer to the struct inside the struct?
> >>> typedef struct A {
> >>> A *p1; /* not allowed */
> >>> B *p2; /* not allowed */
> >>> } B;
> >>
> >> Hum, why don't you first define the type incompletely ?
> >>
> >> typedef struct A A;
> >> struct A {
> >> A *p1;
> >> A *p2;
> >> };

> >
> > 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.




On Saturday, June 9, 2012 2:31:00 AM UTC+8, James Kuyper wrote:
> On 06/08/2012 01:20 PM, 寮*婧 瑗垮寳宸ヤ笟澶у* wrote:
> > On Friday, June 1, 2012 3:14:32 AM UTC+8, Xavier Roche wrote:
> >> Le 31/05/2012 21:10, Kulin a 茅crit :
> >>> What if I want to create a pointer to the struct inside the struct?
> >>> typedef struct A {
> >>> A *p1; /* not allowed */
> >>> B *p2; /* not allowed */
> >>> } B;
> >>
> >> Hum, why don't you first define the type incompletely ?
> >>
> >> typedef struct A A;
> >> struct A {
> >> A *p1;
> >> A *p2;
> >> };

> >
> > 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.




On Saturday, June 9, 2012 2:31:00 AM UTC+8, James Kuyper wrote:
> On 06/08/2012 01:20 PM, 寮*婧 瑗垮寳宸ヤ笟澶у* wrote:
> > On Friday, June 1, 2012 3:14:32 AM UTC+8, Xavier Roche wrote:
> >> Le 31/05/2012 21:10, Kulin a 茅crit :
> >>> What if I want to create a pointer to the struct inside the struct?
> >>> typedef struct A {
> >>> A *p1; /* not allowed */
> >>> B *p2; /* not allowed */
> >>> } B;
> >>
> >> Hum, why don't you first define the type incompletely ?
> >>
> >> typedef struct A A;
> >> struct A {
> >> A *p1;
> >> A *p2;
> >> };

> >
> > 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.




On Saturday, June 9, 2012 2:31:00 AM UTC+8, James Kuyper wrote:
> On 06/08/2012 01:20 PM, 寮*婧 瑗垮寳宸ヤ笟澶у* wrote:
> > On Friday, June 1, 2012 3:14:32 AM UTC+8, Xavier Roche wrote:
> >> Le 31/05/2012 21:10, Kulin a 茅crit :
> >>> What if I want to create a pointer to the struct inside the struct?
> >>> typedef struct A {
> >>> A *p1; /* not allowed */
> >>> B *p2; /* not allowed */
> >>> } B;
> >>
> >> Hum, why don't you first define the type incompletely ?
> >>
> >> typedef struct A A;
> >> struct A {
> >> A *p1;
> >> A *p2;
> >> };

> >
> > 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.


thank you.i know a lot .
what do the same representation and alignment requirements mean?
 
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
CRTP-problem: How can the base class typedef a derived class' typedef? oor C++ 0 05-20-2008 12:39 PM
typedef struct {} SName; vs. struct SName{}; Steven T. Hatton C++ 2 08-03-2005 09:59 AM
Typedef A references struct B which references struct A which... DanielEKFA C++ 8 05-16-2005 10:26 AM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments