code_junkie

Casting between structs?

Nov 12th, 2011
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. typedef struct {
  2. unsigned int hi, lo;
  3. } int64;
  4.  
  5. int64 int64_add(int64 x, int64 y);
  6.  
  7. #define int64_add(x, y) ((x) + (y))
  8.  
  9. typedef int64 mytime;
  10.  
  11. #define mytime_add(x, y) (mytime) int64_add((int64) (x), (int64) (y))
  12.  
  13. typedef struct int64 int64;
  14.  
  15. struct int64
  16. {
  17. unsigned int hi, lo;
  18. };
  19.  
  20. typedef int64 mytime;
  21.  
  22. int64
  23. add_int64(int64 a, int64 b)
  24. {
  25. int64 c;
  26. /* I know that is wrong */
  27. c.hi = a.hi + b.hi;
  28. c.lo = a.lo + b.lo;
  29.  
  30. return c;
  31. }
  32.  
  33. int
  34. main(void)
  35. {
  36. mytime a = {1, 2};
  37. mytime b = {3, 4};
  38. mytime c;
  39.  
  40. c = add_int64(a, b);
  41.  
  42. return 0;
  43. }
  44.  
  45. #define mytime_add(x, y) int64_add((x), (y))
  46.  
  47. mytime mytime_add(mytime x, mytime y) { return int64_add(x, y); }
  48.  
  49. #define mytime int64
Add Comment
Please, Sign In to add comment