Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #154097

Re: pointer to pointer to struct

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: pointer to pointer to struct
Date 2020-08-28 05:12 -0700
Organization A noiseless patient Spider
Message-ID <86imd39dgf.fsf@linuxsc.com> (permalink)
References <9f107dee-440f-4201-9f9d-e0d3754354cfo@googlegroups.com> <zizTG.1051037$6_j.301798@fx37.ams4> <slrnrhthjj.kpi.grahn+nntp@frailea.sa.invalid>

Show all headers | View raw


Jorgen Grahn <grahn+nntp@snipabacken.se> writes:

> On Mon, 2020-07-27, Bart wrote:
>
>> On 27/07/2020 12:42, G G wrote:
>>
>>> #include <stdio.h>
>>> #include <stdlib.h>
>>>
>>> struct bike
>>> {
>>>      int numberOfWheels;
>>>      enum { red, blue, green, black } color;
>>>      enum { mountain, racing, cruiser } bikeType;
>>>      int bike_height;
>>>      int tireSize;
>>> };
>>>
>>> typedef struct bike *bike_ptr;
>>>
>>> int main()
>>> {
>>>       bike_ptr s  =  malloc( sizeof( *s ) );
>>>
>>>      printf("%lu\n", sizeof( *s ) );
>>> }
>>>
>>>
>>> *s is a pointer to a pointer to struct bike in
>>> the malloc(sizeof(*s)), so the struct bike space is
>>> created/ allocated?  right?
>>
>> s is a pointer to the struct.
>>
>> *s (inside an expression) dereferences the pointer, so it is the
>> struct itself.
>>
>>
>> BTW a better way to define this stuff is like this:
>>
>>   typedef struct
>>   {
>>        int numberOfWheels;
>>        enum { red, blue, green, black } color;
>>        enum { mountain, racing, cruiser } bikeType;
>>        int bike_height;
>>        int tireSize;
>>   } bike;
>>
>>
>>    bike b;            // b is a bike struct
>>    bike* s;           // s is a pointer to the struct, *s is the struct
>
> Better IMO to skip the typedefs entirely, and just talk about "struct
> bike".  

Do you think that's always true in all cases, or is this just a
comment about the one particular program?  Is it never okay to
use typedef in connection with structs, or are there some cases
where you would find it acceptable or perhaps even advisable?

Back to comp.lang.c | Previous | Next | Find similar


Thread

Re: pointer to pointer to struct Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-08-28 05:12 -0700

csiph-web