Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

typedef struct

 
 
Nomen Nescio
Guest
Posts: n/a
 
      06-01-2012
Keith Thompson <(E-Mail Removed)> wrote:

> Kulin <(E-Mail Removed)> writes:
> [...]
> > 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;
> >
> > This gives compilation errors. Is it possible to create a pointer to the
> > struct being defined, within the struct, using the original typedef and
> > thereby not having to code 'struct' to define the pointer?

> [snip]
> > My desire is simply to avoid having to create pointers as struct * inside
> > the structure. Other than that I would tend to agree.

>
> Why do you want to avoid using the "struct" keyword?


If you have a struct with several self-referential pointers it looks
cluttered to have to say struct struct struct. Really no more than that.

> But if you want to use a typedef, there's no real need to use different
> identifiers for the struct tag and the typedef name. This:
>
> typedef struct A A;
>
> is perfectly valid and doesn't create any potential ambiguity. Or at
> least use a consistent convention, like:
>
> typedef struct A_ A;


Thanks a lot!

Carl
















 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      06-01-2012
Nomen Nescio <(E-Mail Removed)> writes:
> Keith Thompson <(E-Mail Removed)> wrote:
>> Kulin <(E-Mail Removed)> writes:

[...]
>> Why do you want to avoid using the "struct" keyword?

>
> If you have a struct with several self-referential pointers it looks
> cluttered to have to say struct struct struct. Really no more than that.

[...]
> Carl


My question was directed to the previous poster, Kulin. Are you posting
under multiple names?

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail 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"
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      06-01-2012
On 06/ 2/12 06:45 AM, Keith Thompson wrote:
> Nomen Nescio<(E-Mail Removed)> writes:
>> Keith Thompson<(E-Mail Removed)> wrote:
>>> Kulin<(E-Mail Removed)> writes:

> [...]
>>> Why do you want to avoid using the "struct" keyword?

>>
>> If you have a struct with several self-referential pointers it looks
>> cluttered to have to say struct struct struct. Really no more than that.

> [...]
>> Carl

>
> My question was directed to the previous poster, Kulin. Are you posting
> under multiple names?


He or she certainly does on other groups and mail lists.

--
Ian Collins
 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      06-01-2012
On 2012-06-01, Keith Thompson <(E-Mail Removed)> wrote:
> Nomen Nescio <(E-Mail Removed)> writes:
>> Keith Thompson <(E-Mail Removed)> wrote:
>>> Kulin <(E-Mail Removed)> writes:

> [...]
>>> Why do you want to avoid using the "struct" keyword?

>>
>> If you have a struct with several self-referential pointers it looks
>> cluttered to have to say struct struct struct. Really no more than that.

> [...]
>> Carl

>
> My question was directed to the previous poster, Kulin. Are you posting
> under multiple names?


Remember "George Orwell", "Borked Pseudo Mailed", "Han from China" et al. ?
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      06-01-2012
"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Nomen Nescio <(E-Mail Removed)> writes:
>> Keith Thompson <(E-Mail Removed)> wrote:
>>> Kulin <(E-Mail Removed)> writes:

> [...]
>>> Why do you want to avoid using the "struct" keyword?

>>
>> If you have a struct with several self-referential pointers it looks
>> cluttered to have to say struct struct struct. Really no more than that.

> [...]
>> Carl

>
> My question was directed to the previous poster, Kulin. Are you posting
> under multiple names?


"Nomen Nescio" said on another group that he/she uses 'anonymous remailers'.
But with no warning that they will be using different names, and no
signature on the posts either, things get confusing.

--
Bartc

 
Reply With Quote
 
Fritz Wuehler
Guest
Posts: n/a
 
      06-02-2012
Keith Thompson <(E-Mail Removed)> wrote:

> Nomen Nescio <(E-Mail Removed)> writes:
> > Keith Thompson <(E-Mail Removed)> wrote:
> >> Kulin <(E-Mail Removed)> writes:

> [...]
> >> Why do you want to avoid using the "struct" keyword?

> >
> > If you have a struct with several self-referential pointers it looks
> > cluttered to have to say struct struct struct. Really no more than that.

> [...]
> > Carl

>
> My question was directed to the previous poster, Kulin. Are you posting
> under multiple names?


No, I'm posting as myself but it gets posted through various remailers!

Thanks for the help!

Carl

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-03-2012
Fritz Wuehler <(E-Mail Removed)>
writes:
> Keith Thompson <(E-Mail Removed)> wrote:
>> Nomen Nescio <(E-Mail Removed)> writes:
>> > Keith Thompson <(E-Mail Removed)> wrote:
>> >> Kulin <(E-Mail Removed)> writes:

>> [...]
>> >> Why do you want to avoid using the "struct" keyword?
>> >
>> > If you have a struct with several self-referential pointers it looks
>> > cluttered to have to say struct struct struct. Really no more than that.

>> [...]
>> > Carl

>>
>> My question was directed to the previous poster, Kulin. Are you posting
>> under multiple names?

>
> No, I'm posting as myself but it gets posted through various remailers!
>
> Thanks for the help!


Would you consider adding some consistent header line to all your posts
so we can tell it's you?

--
Keith Thompson (The_Other_Keith) (E-Mail 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"
 
Reply With Quote
 
张源 西北工业大学
Guest
Posts: n/a
 
      06-08-2012
On Friday, June 1, 2012 3:14:00 AM UTC+8, Eric Sosman wrote:
> On 5/31/2012 3:10 PM, Kulin wrote:
> > Keith Thompson<(E-Mail Removed)> wrote:
> >
> >> John McDermick<(E-Mail Removed)> writes:
> >>> If I have this definition in a header file:
> >>>
> >>> typedef struct A
> >>> {
> >>> int test;
> >>> } B;
> >>>
> >>> What exactly is A and what is B ?

> >
> > 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;
> >
> > This gives compilation errors. Is it possible to create a pointer to the
> > struct being defined, within the struct, using the original typedef and
> > thereby not having to code 'struct' to define the pointer?
> >
> >> struct A {
> >> struct A *p;
> >> };

> >
> >
> >> and then refer to it as "struct A". There's no real need to use a
> >> typedef to give the type another name; it already has a perfectly good
> >> one.
> >>
> >> (The counterargument is that many people like to have a one-word name
> >> for the type.)

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

> struct A { // "completes" the type `struct A'
> int test;
> B *p1;
> };




>
> --
> Eric Sosman
> (E-Mail Removed)d



On Friday, June 1, 2012 3:14:00 AM UTC+8, Eric Sosman wrote:
> On 5/31/2012 3:10 PM, Kulin wrote:
> > Keith Thompson<(E-Mail Removed)> wrote:
> >
> >> John McDermick<(E-Mail Removed)> writes:
> >>> If I have this definition in a header file:
> >>>
> >>> typedef struct A
> >>> {
> >>> int test;
> >>> } B;
> >>>
> >>> What exactly is A and what is B ?

> >
> > 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;
> >
> > This gives compilation errors. Is it possible to create a pointer to the
> > struct being defined, within the struct, using the original typedef and
> > thereby not having to code 'struct' to define the pointer?
> >
> >> struct A {
> >> struct A *p;
> >> };

> >
> >
> >> and then refer to it as "struct A". There's no real need to use a
> >> typedef to give the type another name; it already has a perfectly good
> >> one.
> >>
> >> (The counterargument is that many people like to have a one-word name
> >> for the type.)

> >
> > 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
> struct A { // "completes" the type `struct A'
> int test;
> B *p1;
> };
>
> --
> Eric Sosman
> (E-Mail Removed)d




On Friday, June 1, 2012 3:14:00 AM UTC+8, Eric Sosman wrote:
> On 5/31/2012 3:10 PM, Kulin wrote:
> > Keith Thompson<(E-Mail Removed)> wrote:
> >
> >> John McDermick<(E-Mail Removed)> writes:
> >>> If I have this definition in a header file:
> >>>
> >>> typedef struct A
> >>> {
> >>> int test;
> >>> } B;
> >>>
> >>> What exactly is A and what is B ?

> >
> > 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;
> >
> > This gives compilation errors. Is it possible to create a pointer to the
> > struct being defined, within the struct, using the original typedef and
> > thereby not having to code 'struct' to define the pointer?
> >
> >> struct A {
> >> struct A *p;
> >> };

> >
> >
> >> and then refer to it as "struct A". There's no real need to use a
> >> typedef to give the type another name; it already has a perfectly good
> >> one.
> >>
> >> (The counterargument is that many people like to have a one-word name
> >> for the type.)

> >
> > 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
> struct A { // "completes" the type `struct A'
> int test;
> B *p1;
> };
>
> --
> Eric Sosman
> (E-Mail Removed)d


 
Reply With Quote
 
张源 西北工业大学
Guest
Posts: n/a
 
      06-08-2012
On Friday, June 1, 2012 3:14:32 AM UTC+8, Xavier Roche wrote:
> Le 31/05/2012 21:10, Kulin a 閏rit :
> > 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)
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      06-08-2012
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
 
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