Search code examples
c++ctypedef

what is the use of using the typedef and what does it signifies?


I came across the code and it was kind of strange to me I am unable to understand what it means.

typedef struct alpha *Abc;

this is where Abc is defined;

struct alpha{
     Abc s;//what does this mean
}

also some where in function it is used like this

Abc iAmFunc(Abc beta,int a){
       some thing is performed
       return variable of type Abc
}

I dont understand the purpose of doing this can anyone explain me!!


Solution

  • Note, this is for C and not C++. Maybe it's applicable for C++ too, but I don't know.

    typedef a b makes b an alias for a

    What typedef struct alpha *Abc; is to make Abc an alias for struct alpha *. After you have created this, these two are equivalent:

    struct alpha *ptr;
    Abc ptr;
    

    There are a few different uses for this:

    It reduces how much you need to write. You can skip the struct word for instance. Many would say that this is NOT a good reason to use typedef.

    It can give a more descriptive name for certain things. Examples:

    typedef char* string; // I would never use this. It's just an example.
    typedef int[3] vector; // However, this is something I would consider.
    

    It can make it easier to change the type for a large code base in certain situations. Suppose you have this code:

    int16_t foo(int16_t a)
    {
        int16_t b = a+1;
        for(int16_t = 0; i<b; i++) {
    ...
    

    And later you realize that it would be a good idea to change to int32_t instead. If the code instead looked like this, you would only need to change one line:

    typedef int16_t T
    T foo(T a)
    {
        T b = a+1;
        for(T = 0; i<b; i++) {
    ...
    

    Be restrictive with typedefs. Often they just clutters the code and makes it harder to follow. Use them when they fill a purpose. Be especially restrictive with aliasing pointers. I only use them for function pointers and completely opaque objects. Same thing with structs and unions. IMHO, 99% of the typedefs I see in questions here at SO are not necessary and seems to be there just because people think that they should.

    If you consider typedefing a struct, ask yourself if it would be good to anyone reading the code to know that a declaration is a struct. If yes, declare it with the struct keyword. Of course you could invent something like typedef struct my_struct my_struct_s but is it really worth the effort? I'd say no.

    It can be worth noting that C has something that seems like a simple form of namespace. This means that this completely valid:

    typedef struct mystruct mystruct;
    struct mystruct a;
    mystruct b;
    

    So mystruct is an alias for mystruct. And this is what you should do, unless you have a good reason not to.

    I once wrote a related answer and if that answer wasn't so heavily focused on just structs, I would have made this question a duplicate.

    In comments below, I saw this example:

    typedef unsigned short int int_16;
    

    and this is a HORRIBLE example. First, int_16 gives the impression that it is a signed type, which it is clearly not. Secondly, short int (and its unsigned counterpart) is guaranteed to have at least 16 bits, but int_16 gives the impression that it have exactly 16 bits. Depending on what you want, there are a few good ones in the standard:

    • int16_t and uint16_t - Exactly 16 bits
    • int_least16_t and uint_least16_t - At least 16 bits
    • int_fast16_t and uint_fast16_t - At least 16 bits, but hint to the compiler that you want the fastest type