News for hackers. Stuff that matters.
Re:Modern C for C++ Peeps (2019) by bluetomcat on Wednesday May 26, 2021 @12:23PM (#27292473)
> Wrap your structs in a typedef

I tend to disagree. Unless you are providing an API for third-party use and want to make your data types opaque to callers, do not ever use typedef with structs and pointers. A declaration like "struct foo *f" tells you two things - f itself is a pointer, and the pointed-to value is not a scalar (i.e. has named members). The "_t" suffix idiom is predominantly used by the standard library for integral scalars with certain properties and semantics, e.g. pid_t, uid_t, gid_t, time_t.

Re:Modern C for C++ Peeps (2019) by visualradio on Wednesday May 26, 2021 @01:50PM (#27293791)
If programmers do want to wrap structs in a typedef without using the "_t" suffix, the Vulkan API appears to have settled on upper-camel case:

    typedef struct VkMemoryBarrier {
        VkStructureType    sType;
        const void*        pNext;
        VkAccessFlags      srcAccessMask;
        VkAccessFlags      dstAccessMask;
    } VkMemoryBarrier;
Re:Modern C for C++ Peeps (2019) by ynfnehf on Wednesday May 26, 2021 @12:39PM (#27292719)
The _t suffix is also reserved by posix. (And by the C standard if the name starts with int or unit.)

I agree with just using struct foo, as does the Linux style guide.

(Edit: I just noticed the page mentions this, but it is perhaps useful to reiterate.)

Re:Modern C for C++ Peeps (2019) by mlex on Wednesday May 26, 2021 @01:19PM (#27293337)
Somewhat related; I feel like C reserves a lot of names that though different standards, is there a good list of all of those so I know to avoid them? (e.g. underscore prefixes)
Re:Modern C for C++ Peeps (2019) by zabzonk on Wednesday May 26, 2021 @01:28PM (#27293473)
This part of the GCC docs covers all of them, AFAIK:

https://www.gnu.org/software/libc/manual/html_node/Reserved-...

Re:Modern C for C++ Peeps (2019) by jimbob45 on Wednesday May 26, 2021 @12:38PM (#27292712)
Agreed. If you typedef your struct, I can’t know if you wrapped it in a weird macro or if your data type is actually a renamed primitive. Once I gain familiarity with your codebase, it won’t matter, but it otherwise unnecessarily raises the barrier of entry.

That said, typedefs/macros may be essential for portability concerns.

Re:Modern C for C++ Peeps (2019) by Gibbon1 on Wednesday May 26, 2021 @02:40PM (#27294419)
> can’t know if you wrapped it in a weird macro

The solution to that problem is don't do that.

Re:Modern C for C++ Peeps (2019) by IanGabes on Wednesday May 26, 2021 @02:50PM (#27294527)
Huh. This is interesting, I can empathize with both sides of the argument. Are C text editors not able to take care of syntax highlighting the differences between a typedef'd scalar and a typedef'd struct? Colouring the type one way or another I figure would be sufficient.