Thread: typedef issue in c

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    24

    typedef issue in c

    Greeting to Everyone,

    I have been asked by my friend the explanation of the usage and functionality of this typedef
    Code:
    (highlighted as //Issue01)
    but it seems complicated to me as well.

    Code:
    #define INDEX_MASK 0x0000FFFF
    typedef struct
    {
       U8  data[4];
       U16 crc;
    } term_sf_param; 
    
    
    typedef const struct term_titem_par_s
    {
       term_titype_t itype;
       void* p_val;
       term_limits_t lim;
       U32 identifier;
    } term_titem_par_t, *term_pitem_par;
    
    
    typedef const struct _term_titem_s
    {
      term_titype_t itype;
    } term_titem_t, *term_pitem;
    
    
    static term_sf_param *para_set0 = NULL;
    
    
    term_sf_param term_set_get(term_pitem p_item_new)
    {
      term_sf_param  par0 = para_set0[((term_pitem_par)p_item_new)->identifier & INDEX_MASK]; //Issue02
    
    
      return par0;
    }
    
    
    typedef S16 (*ifunci)(S16);
    
    
    typedef  ifunci const *term_titype_t;     //Issue01
    
    
    typedef struct
    {
       U8  data[4];
       U16 crc;
    } term_sf_param;
    At this point, I don't understand how will this typedef works? Is it just associating a name
    Code:
    "const *term_titype_t"
    to function pointer
    Code:
    "S16 (*ifunci)(S16)"
    ?? It does sound unclear and some part may be in typedef I have either missed or not understood completely.. can anybody explain how this thing works?


    Additionally in function
    Code:
    "term_set_get"
    , the input argument is "p_item_new" whose datatype is "term_pitem". Inside the function, that same argument is typecasted with structure
    Code:
    "term_pitem_par"
    and based on the values, it updates "para_set0".


    It seems awkward as
    Code:
    sizeof(term_pitem) < sizeof(term_pitem_par)
    . Would be doing that is correct? Is this concept related to data hiding (its my assumption) as I have never encountered this type casting before.


    Any link or description related to my problem will also be appreciable.


    Thanks in advance..

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    I don't get the first issue either. I don't see what another level of pointers achieves. Can you show the code where itype is set and where it is called?

    For the second issue, the fact that the casted-to struct is bigger is not a problem since we are accessing it through a pointer. Also note that the first member is the same in both structs, so this is a kind of polymorphism in C where the smaller struct is the "base class" and the larger struct extends it with extra fields. Presumably there are other structs that extend it with different fields.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It helps to copy the very bare minimum into a file and try it.
    Code:
    #include <stdio.h>
    
    // a function pointer
    typedef int (*ifunci)(int);
    
    // a const pointer to a function pointer
    typedef  ifunci const *term_titype_t; 
    
    int foo ( int bar ) {
        return printf("%d\n", bar);
    }
    
    int main()
    {
        ifunci  p1 = foo;
        term_titype_t   t = &p1;
        printf("%d\n", p1(42));
        printf("%d\n", (*t)(999));
        return 0;
    }
    
    
    $ gcc foo.c
    $ ./a.out 
    42
    3
    999
    4
    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.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    So putting it all together, with simplified names.
    Still I wonder what the point of the second level of indirection might be.
    Code:
    #include <stdio.h>
     
    typedef int (*PF)(int);
    typedef PF const *PPF;
     
    typedef const struct _S2 {
        PPF f;
        int a;
        int b;
    } S2, *PS2;
     
    typedef const struct _S {
        PPF f;
    } S, *PS;
     
    int func(int n) {
        return printf("%d\n", n);
    }
     
    void use(PS ps) {
        PS2 ps2 = (PS2)ps;
        (*ps2->f)(1);
        printf("%d %d\n", ps2->a, ps2->b);
    }
     
    int main() {
        PF pf = func;
        PPF ppf = &pf;
        S2 s2 = (S2){ppf, 2, 3};
        use((PS)&s2);
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    24
    Quote Originally Posted by john.c View Post
    I don't get the first issue either. I don't see what another level of pointers achieves. Can you show the code where itype is set and where it is called?

    For the second issue, the fact that the casted-to struct is bigger is not a problem since we are accessing it through a pointer. Also note that the first member is the same in both structs, so this is a kind of polymorphism in C where the smaller struct is the "base class" and the larger struct extends it with extra fields. Presumably there are other structs that extend it with different fields.
    Sorry friend, it seems that the element

    "itype"

    of structure "term_titem_t" are not directly assigning and being called anywhere in code. It is only been used as argument to function.
    Also is not it is better to define data-type of function argument as "term_pitem_par" rather "term_pitem"?

  6. #6
    Registered User
    Join Date
    Oct 2013
    Posts
    24
    Hi Salem,

    Thanks for the post, but I did not get your answer..

    Could you please explain. Any particular objective to do such level of "misdirection" (with no comments at all...at-least it is what it seems to me). Isn't its an example of bad programming? as
    Quote Originally Posted by Salem View Post
    It helps to copy the very bare minimum into a file and try it.
    Code:
    #include <stdio.h>
    
    // a function pointer
    typedef int (*ifunci)(int);
    
    // a const pointer to a function pointer
    typedef  ifunci const *term_titype_t; 
    
    int foo ( int bar ) {
        return printf("%d\n", bar);
    }
    
    int main()
    {
        ifunci  p1 = foo;
        term_titype_t   t = &p1;
        printf("%d\n", p1(42));
        printf("%d\n", (*t)(999));
        return 0;
    }
    
    
    $ gcc foo.c
    $ ./a.out 
    42
    3
    999
    4

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with pointer to a const char using typedef
    By krkr in forum C Programming
    Replies: 3
    Last Post: 07-24-2015, 06:53 AM
  2. typedef issue
    By quo in forum C++ Programming
    Replies: 16
    Last Post: 05-09-2012, 03:05 PM
  3. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-05-2010, 11:52 PM
  4. typedef
    By sick in forum C Programming
    Replies: 6
    Last Post: 12-10-2009, 12:48 AM
  5. typedef issue
    By Drac in forum C++ Programming
    Replies: 4
    Last Post: 07-19-2009, 04:41 PM

Tags for this Thread