Thread: typedef

  1. #1
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    64

    typedef

    Is there a rule of thumb for creating typedef's or is there a link that has best practices for typedef'ing in C programs?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,122
    None that I've see, so this is just IMO.

    Redefining integer types is a waste of effort in a post C99 world when you have these available.
    Fixed width integer types (since C99) - cppreference.com

    structs which are part of a public interface should be typedef'd.
    A private struct hidden away that's only used in a couple of places I might leave as a bare struct.

    Function pointer types should always be typedef'd.
    Seriously, it gets messy in a hurry.
    Code:
    #include <stdio.h>
    
    int foo(char *a, int b) {
        return printf("foo called %s %d\n", a, b);
    }
    int bar(char *a, int b) {
        return printf("bar called %s %d\n", a, b);
    }
    
    int(*p1)(char*,int) = foo;
    
    int(*(a1[]))(char*,int) = {
        foo,
        bar,
    };
    
    int(*(lookup1(int index)))(char*,int) {
        return a1[index];
    }
    
    // VS
    
    typedef int(*fptr)(char*,int);
    fptr p2 = foo;
    fptr a2[] = {
        foo,
        bar,
    };
    fptr lookup2(int index) {
        return a2[index];
    }
    
    int main() {
        p1("yah",1);
        lookup1(1)("boo",2);
        p2("yah",1);
        lookup2(1)("boo",2);
    }
    With a typedef, a function pointer just looks like any other regular type.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    64
    Quote Originally Posted by Salem View Post
    Seriously, it gets messy in a hurry.
    I have to agree with you on this point.

    I think your suggestions are a good starting point.

  4. #4
    Registered User
    Join Date
    Apr 2021
    Posts
    92
    Typedefs have managed to become controversial, for who-knows-what-real-reason. The Linux coding standard decries their use. They prefer that all "struct Foo" be spelled out completely. (Not sure how they feel about non-struct typedefs, though.)

    For myself, I believe that typedefs are useful and almost always appropriate, provided they are typedefs related to your problem space. As @Salem points out, pointer types are a good place to start because they can also serve to create a single-word name from a multi-part name. (This makes it possible to use macro token-pasting to good effect later on.)

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,122
    I'm only suggesting typedefs for function pointer types.

    Regular pointers to data types should not be typedef'd.

    Example.
    Suppose you have
    typedef int *iptr;

    What if you want a const pointer.
    No problem, you just do
    const iptr var; // aka const int *iptr;

    All well and good, but what if you want the pointer itself to be const?
    You can't do that with the typedef.
    So you have to retreat to long-hand anyway
    int * const iptr;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    817
    Quote Originally Posted by Salem View Post
    I'm only suggesting typedefs for function pointer types.

    Regular pointers to data types should not be typedef'd.

    Example.
    Suppose you have
    typedef int *iptr;

    What if you want a const pointer.
    No problem, you just do
    const iptr var; // aka const int *iptr;

    All well and good, but what if you want the pointer itself to be const?
    You can't do that with the typedef.
    So you have to retreat to long-hand anyway
    int * const iptr;
    I'm no expert on typedef, but this code:

    Code:
    typedef int *iptr;
    typedef const int *ciptr;
    
    const int foo = 42;
    const iptr p1 = &foo;
    ciptr p2 = &foo;
    will give this warning:

    Code:
    180912.c:5:17: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
        5 | const iptr p1 = &foo;
          |                 ^
    which suggests that p1 itself is const and points to a non-const int. On the other hand, p2 is a non-const pointer to const int.

  7. #7
    Registered User
    Join Date
    Apr 2021
    Posts
    92
    Quote Originally Posted by Salem View Post
    Regular pointers to data types should not be typedef'd.

    Example.
    Suppose you have
    typedef int *iptr;

    What if you want a const pointer.
    You mean, "What if you want a different type?" :-) That would be a different typedef,
    unless you were just trying to indicate that the variable you declared was unchanging, which
    const does just fine.

    Code:
    typedef struct Foo {
        int x ;
    } Foo;
    
    Foo foo;
    const Foo cfoo;
    
    typedef int * IntStar;
    
    IntStar iptr;
    const IntStar ciptr;
    
    void
    test(void)
    {
        int dummy;
    
        foo.x = 1;
        cfoo.x = 2;
    
        iptr = &dummy;
        ciptr = &dummy;
    
        *iptr = 0;
        *ciptr = 1;
    }
    Compilation:

    Code:
    $ gcc -std=c17 -Wall -fsyntax-only cc.c
    cc.c: In function ‘test’:
    cc.c:19:9: error: assignment of member ‘x’ in read-only object
      cfoo.x = 2;
             ^
    cc.c:22:8: error: assignment of read-only variable ‘ciptr’
      ciptr = &dummy;
            ^
    In general, though, I disagree with your sentiment. A pointer type should be typedef'ed if the pointer is the type. When you're allocating an array of objects, and intend to iterate through the array possibly using a pointer, the pointer is not the type -- the object is the type. And so you should typedef the object and use Object * for the pointer.

    But when you're returning an opaque object that happens to be a pointer, and it's nobody's business what's behind the pointer, then by all means typedef the pointer. Consider for example the stdio type FILE. I've never written application code that dereferences or accesses through a FILE *. In a modern C library rewrite, FILE * should be typedef'ed to File or file_t something, since it's opaque and nobody really needs to know what's behind it. If all you do is pass them around, you can certainly hide the pointer-ness in a typedef.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,407
    Quote Originally Posted by aghast
    If all you do is pass them around, you can certainly hide the pointer-ness in a typedef.
    Yeah, I hold that opinion too, as my objection to object pointer typedefs has to do with pointer notation such as dereferencing the pointer then becoming surprising. For opaque pointers, neither any argument about const-ness nor about notion is applicable, so they are excellent candidates for a pointer typedef. But your post #4 did not make such a distinction, so I can see why Salem objected.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Typedef
    By ncode in forum C Programming
    Replies: 4
    Last Post: 01-26-2012, 03:31 AM
  2. Typedef
    By vijay s in forum C Programming
    Replies: 2
    Last Post: 11-05-2009, 01:00 PM
  3. Typedef
    By Sarah_SE in forum C Programming
    Replies: 10
    Last Post: 11-24-2005, 03:45 PM
  4. How to typedef and use it.
    By Roaring_Tiger in forum C Programming
    Replies: 3
    Last Post: 02-09-2005, 01:50 PM
  5. What does 'typedef' do?
    By supaben34 in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2002, 07:18 PM

Tags for this Thread