Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 11th, 2012  |  syntax: None  |  size: 0.69 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Struct pointer compatibility
  2. typedef struct Struct1
  3. {
  4.     short a_short;
  5.     int id;
  6. } Struct1;
  7.  
  8. typedef struct Struct2
  9. {
  10.     short a_short;
  11.     int id;
  12.     short another_short;
  13. } Struct2;
  14.        
  15. typedef struct {
  16.     Struct1 struct1;
  17.     short another_short;
  18. } Struct2;
  19.        
  20. #include <stdio.h>
  21.  
  22. typedef struct Struct1
  23. {
  24.     short a_short;
  25.     int id;
  26. } Struct1;
  27.  
  28. typedef struct Struct2
  29. {
  30.     short a_short;
  31.     int id;
  32.     short another_short;
  33. } Struct2;
  34.  
  35. int main(void)
  36. {
  37.  
  38.     Struct2 s2 = {1, 2, 3};
  39.     Struct1 *ptr = &s2;
  40.     void *vp = &s2;
  41.     Struct1 *s1ptr = (Struct1 *)vp;
  42.  
  43.     printf("%d, %d n", ptr->a_short, ptr->id);
  44.     printf("%d, %d n", s1ptr->a_short, s1ptr->id);
  45.  
  46.     return 0;
  47. }