Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: pointer to pointer to struct Date: Fri, 28 Aug 2020 05:12:16 -0700 Organization: A noiseless patient Spider Lines: 60 Message-ID: <86imd39dgf.fsf@linuxsc.com> References: <9f107dee-440f-4201-9f9d-e0d3754354cfo@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="9035d0fd7127b99c8fa8fe8c15caa128"; logging-data="7319"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/61v/gPmT1TQM/hkJ7WWSssh7ytNXAx6I=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:byN+zC0naxsgEPG8wq3bbibaqbk= sha1:kHIjnogf+FXOs3FCKlfc1EJbYXc= Xref: csiph.com comp.lang.c:154097 Jorgen Grahn writes: > On Mon, 2020-07-27, Bart wrote: > >> On 27/07/2020 12:42, G G wrote: >> >>> #include >>> #include >>> >>> 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?