variable type declaration

This is a discussion on variable type declaration within the C Programming forums, part of the General Programming Boards category; Code: u_short in_cksum(unsigned short *addr, int len); u_long resolv(char *host); i know what short and long are, but what does ...

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    variable type declaration

    Code:
    u_short in_cksum(unsigned short *addr, int len);
    u_long resolv(char *host);
    i know what short and long are, but what does the "u_" do there?


    ive been wondering about these aswell
    Code:
    struct pseudohdr {
    u_int32_t saddr;
    u_int32_t daddr;
    u_int8_t zero;
    u_int8_t protocol;
    u_int16_t length;
    } *pseudohdr;
    weird

    thanks

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    unsigned?

    Its a non standard implementation, if you want to know for sure that they are, search your header files for the typedef's.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    xmdvp
    Guest
    'u' always stands for unsigned
    32, 8, and 16 stand for how many bits per variable:

    if char is 8 bit

    then I would do:

    Code:
    typedef unsigned char u_int8_t
    
    typedef struct {
       unsigned char1 x;
       unsigned char2 y;
    } u_int16_t
    
    typedef struct {
       unsigned char1 x;
       unsigned char2 y;
       unsigned char3 x;
       unsigned char4 y;
    } u_int32_t
    u_int32_t saddr;
    u_int32_t daddr;
    u_int8_t zero;
    u_int8_t protocol;
    u_int16_t length;

  4. #4
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    now it makes sense...
    thanks

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    For something that is called u_int16_t, I wouldn't expect it to be a structure. I would have expected something like this, YMMV.
    Code:
    typedef unsigned short u_short;
    typedef unsigned long  u_long;
    typedef unsigned char  u_int8_t;
    typedef unsigned short u_int16_t;
    typedef unsigned long  u_int32_t;
    I know that the reason for these typedefs is to try make this code easier to port to platforms with different size ints, but in my opinion it just obfuscates code.

    Having said that, this is one of very few situations I break from my avoidance of typedefs. But I would prefer to make the typedefs match the exact-width integer type identifiers defined in C99, with suitable wrappers.
    Code:
    #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901
      #include <stdint.h>
    #else /* typedefs specific to a platform */
      typedef unsigned char  uint8_t;
      typedef unsigned short uint16_t;
      typedef unsigned long  uint32_t;
    #endif

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 02:17 AM
  2. Replies: 6
    Last Post: 07-29-2008, 05:37 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. "error: incomplete type is not allowed"
    By Fahrenheit in forum C++ Programming
    Replies: 9
    Last Post: 05-10-2005, 10:52 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21