Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #158445
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: I need help understand a struct |
| Date | 2021-01-19 01:52 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <87bldl66ci.fsf@bsb.me.uk> (permalink) |
| References | <ru5b0m$rm6$1@dont-email.me> <87h7nd671f.fsf@bsb.me.uk> <ru5dbr$6si$1@dont-email.me> |
T <T@invalid.invalid> writes:
> On 1/18/21 5:37 PM, Ben Bacarisse wrote:
>> T <T@invalid.invalid> writes:
>>
>>> https://gitlab.freedesktop.org/xorg/app/xclipboard/-/blob/master/xclipboard.c
>>>
>>> 66: typedef struct _Clip {
>>> 67: struct _Clip *next, *prev;
>>> 68: char *clip;
>>> 69: char *filename;
>>> 70: size_t avail;
>>> 71: } ClipRec, *ClipPtr;
>>>
>>> Okay now I am confused. What??? Yes, AGAIN!
>>>
>>> 66 defines a structure called "_Clip". So far
>>> so good
>>
>> It's a little bit more than that. The typedef means that then whole
>> declaration will also define some "type names" -- aliases for types.
>>
>> Putting that aside, yes, struct X { ... } is how a structure is defined.
>> The definition says what types of object are contained in an instance of
>> the structure. These are called the members.
>>
>>> 67 look like it is doing it again inside
>>> the original definition. Is this a structure
>>> embedded in another structure reusing the
>>> same name ????
>>
>> Without the { ... } no structure is being defined. Instead, the type
>> "struct _Clip" is used to declare objects -- in this case two members
>> called next and prev. But the type of these members is not "struct
>> _Clip". It is "pointer to struct _Clip". That's what the * in front of
>> the declared name means.
>>
>> So a struct _Clip contains two pointers to other struct _Clip objects.
>> This is clearly going to be a doubly linked list.
>>
>>> 68 & 69 look like they a declaring pointers to
>>> a character. Are not pointers usually
>>> int64's?
>>
>> A pointer a is derived type in C. You can have a pointer to a
"A pointer type is a derived type in C".
>> character, a pointer to an int or, as I've just described, a pointer to
>> a struct object.
>>
>>> 70 What is an "avail"? Some kind of structure?
>>
>> No, it is just an unsigned integer. size_t is the type used by C to
>> represent object sizes.
>>
>>> 71 looks like it is returning two somethings
>>> from the structure definition. Is this a
>>> function or a structure or both?
>>
>> Now we get to the typedef part. Here is a declaration that only says
>> what's in a struct S:
>>
>> struct S { int a; float b };
>>
>> After that, I can declare one or two of them:
>>
>> struct S s, t;
>>
>> but I could have done both together like this:
>>
>> struct S { int a; float b } s, t;
>>
>> And I can also add further decoration to declare some objects with
>> derived types:
>>
>> struct S { int a; float b } s, *p, ar[3];
>>
>> declares a struct S (called s), a pointer (called p) to a struct S and
>> and array of three struct S objects with the name ar.
>>
>> Putting typedef in front of a declaration changes the meaning so that
>> any declared names (like "s" and "p" and "ar" above) become aliases for
>> their types, rather than variables of that type. Thus
>>
>> typedef struct S { int a; float b } s, *p, ar[3];
>>
>> makes "one" an alias for the type "struct S", "p" an alias for
>> "pointer
Makes "s" an alias for the type "struct S". (I changed the name and
did not edit all occurrences.)
>> to a struct S" and "ar" an alias for "array of three struct S".
>> Obviously, in real life, you would not use such short names for types.
>>
>> Can you now see what the declaration is doing?
>>
>
> Hi Ben,
>
> I am going to have to read and reread what you wrote
> several times before the lights go off. Thank you for
> the wonderful write up!
You're welcome. Since you plan to read it more than once, it seems only
fair to correct a couple of mistakes.
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
I need help understand a struct T <T@invalid.invalid> - 2021-01-18 17:01 -0800
Re: I need help understand a struct Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-01-19 01:37 +0000
Re: I need help understand a struct T <T@invalid.invalid> - 2021-01-18 17:41 -0800
Re: I need help understand a struct Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-01-19 01:52 +0000
Re: I need help understand a struct T <T@invalid.invalid> - 2021-01-18 18:03 -0800
Re: I need help understand a struct Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-01-18 19:00 -0800
Re: I need help understand a struct Bart <bc@freeuk.com> - 2021-01-19 11:34 +0000
Re: I need help understand a struct Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-01-19 04:18 -0800
Re: I need help understand a struct Bart <bc@freeuk.com> - 2021-01-19 13:01 +0000
Re: I need help understand a struct Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-01-19 21:39 +0000
Re: I need help understand a struct Bart <bc@freeuk.com> - 2021-01-19 23:20 +0000
Re: I need help understand a struct Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-01-20 00:25 +0000
Re: I need help understand a struct Bart <bc@freeuk.com> - 2021-01-20 12:42 +0000
Re: I need help understand a struct Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-01-20 17:39 +0000
Re: I need help understand a struct Bart <bc@freeuk.com> - 2021-01-20 19:32 +0000
Re: I need help understand a struct Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-01-20 20:45 +0000
Re: I need help understand a struct Bart <bc@freeuk.com> - 2021-01-21 16:12 +0000
Re: I need help understand a struct Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-01-21 17:10 +0000
Re: I need help understand a struct Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-01-21 11:51 -0800
Re: I need help understand a struct Bart <bc@freeuk.com> - 2021-01-22 12:26 +0000
Re: I need help understand a struct Kaz Kylheku <563-365-8930@kylheku.com> - 2021-01-21 20:16 +0000
Re: I need help understand a struct Bart <bc@freeuk.com> - 2021-01-21 23:04 +0000
Re: I need help understand a struct "jfbod...@gmail.com" <jfbode1029@gmail.com> - 2021-01-25 11:59 -0800
Re: I need help understand a struct Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-01-25 21:18 -0800
Re: I need help understand a struct Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-01-27 07:53 -0800
Re: I need help understand a struct Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-01-27 08:35 -0800
Re: I need help understand a struct Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-01-28 07:48 -0800
Re: I need help understand a struct Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-01-28 09:25 -0800
Re: I need help understand a struct Kaz Kylheku <563-365-8930@kylheku.com> - 2021-01-28 18:03 +0000
Re: I need help understand a struct Guillaume <message@bottle.org> - 2021-01-28 20:46 +0100
Re: I need help understand a struct Kaz Kylheku <563-365-8930@kylheku.com> - 2021-01-28 21:31 +0000
Re: I need help understand a struct Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-01-29 11:15 -0800
Re: I need help understand a struct T <T@invalid.invalid> - 2021-01-25 21:20 -0800
csiph-web