Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

typedef struct

 
 
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,i know a lot.
can you explain below for me?
the same representation and alignment requirements
 
Reply With Quote
 
 
 
 
James Kuyper
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:

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


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.
 
Reply With Quote
 
 
 
 
Zhang Yuan
Guest
Posts: n/a
 
      06-08-2012
On Saturday, June 9, 2012 3:10:59 AM UTC+8, James Kuyper wrote:
> > On Saturday, June 9, 2012 2:31:00 AM UTC+8, James Kuyper wrote:
> >> On 06/08/2012 01:20 PM, *源 西北工业大* wrote:

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

>
> 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!
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-08-2012
Zhang Yuan <(E-Mail Removed)> writes:
> On Saturday, June 9, 2012 1:55:22 AM UTC+8, Eric Sosman wrote:

[...]
>> 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 <(E-Mail Removed)>. 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) 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
 
Zhang Yuan
Guest
Posts: n/a
 
      06-09-2012
On Saturday, June 9, 2012 5:22:20 AM UTC+8, Keith Thompson wrote:
> Zhang Yuan <(E-Mail Removed)> writes:
> > On Saturday, June 9, 2012 1:55:22 AM UTC+8, Eric Sosman wrote:

> [...]
> >> 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 <(E-Mail Removed)>. 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 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!
 
Reply With Quote
 
Robert Miles
Guest
Posts: n/a
 
      06-29-2012
On 6/1/2012 1:45 PM, Keith Thompson wrote:
> 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"


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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-29-2012
Robert Miles <(E-Mail Removed)> writes:
> On 6/1/2012 1:45 PM, Keith Thompson wrote:
>> 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"

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


Any particular reason?

--
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
 
Robert Miles
Guest
Posts: n/a
 
      07-02-2012
On 6/29/2012 4:58 PM, Keith Thompson wrote:
> Robert Miles <(E-Mail Removed)> writes:
>> On 6/1/2012 1:45 PM, Keith Thompson wrote:
>>> 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"

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

>
> Any particular reason?


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


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-02-2012
Robert Miles <(E-Mail Removed)> writes:
> On 6/29/2012 4:58 PM, Keith Thompson wrote:
>> Robert Miles <(E-Mail Removed)> writes:
>>> On 6/1/2012 1:45 PM, Keith Thompson wrote:
>>>> 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"
>>>
>>> Looks like someone needs to answer "Pushing you out a rather high window
>>> is something. Therefore, we must do that too."

>>
>> Any particular reason?

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

--
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
 
James Kuyper
Guest
Posts: n/a
 
      07-02-2012
On 07/02/2012 02:00 AM, Keith Thompson wrote:
> Robert Miles <(E-Mail Removed)> writes:
>> On 6/29/2012 4:58 PM, Keith Thompson wrote:
>>> Robert Miles <(E-Mail Removed)> writes:
>>>> On 6/1/2012 1:45 PM, Keith Thompson wrote:
>>>>> 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"
>>>>
>>>> Looks like someone needs to answer "Pushing you out a rather high window
>>>> is something. Therefore, we must do that too."
>>>
>>> Any particular reason?

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


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