Possible useful pointer add macro for C and C++

Discussion in 'General Programming Support' started by tom_mai78101, Sep 7, 2016.

  1. tom_mai78101

    tom_mai78101 The Helper Connoisseur / Ex-MineCraft Host Staff Member

    Ratings:
    +985 / 4 / -1
    You probably seen this macro used in C++:

    Code:
    #define MACRO_PTR_ADD( P, BYTES ) \
        ((decltype( P ))(((u8 *)P) + (BYTES)))
    The (non-tested) equivalent in C is:

    Code:
    #define MACRO_PTR_ADD( P, BYTES ) \
       ((__typeof__( P ))(((u8 *)P) + (BYTES)))
     
  2. jonas

    jonas Well-Known Member

    Ratings:
    +42 / 4 / -0
    why this is necessary is beyond me. but be aware that going out of the limits of the object is undefined behavior.
     
  3. tom_mai78101

    tom_mai78101 The Helper Connoisseur / Ex-MineCraft Host Staff Member

    Ratings:
    +985 / 4 / -1
    For one, porting a C++ code to C code is one actual case for me.
     
  4. jonas

    jonas Well-Known Member

    Ratings:
    +42 / 4 / -0
    Why do you need to port the C++ code to C?
     
  5. tom_mai78101

    tom_mai78101 The Helper Connoisseur / Ex-MineCraft Host Staff Member

    Ratings:
    +985 / 4 / -1
    For various reasons, one of which being there's no C++ compiler for it. Only C. And because of that, all libraries uses only C, and not C++.
     

Share This Page