Thread: #typedef question

  1. #1
    Registered User
    Join Date
    Apr 2020
    Location
    Greater Philadelphia
    Posts
    26

    #typedef question

    I've found a need to reverse the endianness of integer data read from some files I'm working with. So I wrote the following:


    Code:
    typedef unsigned int uint;
    typedef unsigned char byte;
    uint reversendian(uint i)
      {
         union { uint i; byte b[4]; } in, out;
         int j;
         in.i=i; 
         for (j=0; j<4; j++)
           {
             out.b[3-j]=in.b[j];
           }
         return out.i;
      }
    I say "uint" and "byte" because I get tired of typing "unsigned int" and "unsigned char" all the time. These typedefs are actually in the header file for the functions in my own library.

    This function works for my purposes. But the problem, of course, is that it is not portable, because it assumes a 4-byte size for int or unsigned int. What can I use for the type of data of the parameter, returned value, and innards such that it will serve for either signed or unsigned four-byte integers, but a compiler will reject attempts to use it for other sizes?
    Last edited by Alogon; 04-29-2020 at 11:16 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    uint32_t in stdint.h if your compiler is at least C99 will remove some variability.

    But you won't be able to stop people calling it with shorter types because of default promotion rules.
    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
    Apr 2020
    Location
    Greater Philadelphia
    Posts
    26
    Perhaps it should accept a character pointer (to an address in the input buffer or record) instead of an integer argument, since it isn't actually a proper integer with the wrong endianism.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    In your particular case though, you might not need to be fixated on requiring a 4-byte unsigned int: you could replace both uses of 4 with sizeof(uint) and the use of 3 with sizeof(uint)-1. This way your code will work even if unsigned int has a different number of bytes.
    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

  5. #5
    Registered User
    Join Date
    Apr 2020
    Location
    Greater Philadelphia
    Posts
    26
    Good idea! I'll do that. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. typedef question
    By Louis Ferreira in forum Linux Programming
    Replies: 1
    Last Post: 11-24-2015, 01:27 AM
  2. Question on typedef
    By soljiang in forum C Programming
    Replies: 8
    Last Post: 07-09-2014, 12:40 AM
  3. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  4. typedef question
    By Chaplin27 in forum C++ Programming
    Replies: 4
    Last Post: 09-22-2006, 10:42 AM
  5. question about typedef
    By volk in forum C++ Programming
    Replies: 8
    Last Post: 05-30-2003, 10:53 PM

Tags for this Thread