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

Untitled

By: a guest on May 27th, 2012  |  syntax: None  |  size: 1.26 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. incompatible struct types in casts/assignment?
  2. #include <stdio.h>
  3.  
  4. int main(void)
  5. {
  6.   int i;
  7.   int dst[] = { 10, 20, 30 }, src[] = { 1, 2, 3 };
  8.  
  9.   *(struct{int _[3];}*)dst = *(struct{int _[3];}*)src;
  10.  
  11.   for (i = 0; i < 3; i++) printf("%dn", dst[i]);
  12.   return 0;
  13. }
  14.        
  15. #include <stdio.h>
  16. #include <string.h>
  17.  
  18. int main(void)
  19. {
  20.   int i;
  21.   int dst[] = { 10, 20, 30 }, src[] = { 1, 2, 3 };
  22.  
  23.   memcpy(dst, src, 3 * sizeof(int));
  24.  
  25.   for (i = 0; i < 3; i++) printf("%dn", dst[i]);
  26.   return 0;
  27. }
  28.        
  29. #include <stdio.h>
  30.  
  31. int main(void) {
  32.     // struct T is only visible in main()
  33.     struct T {int _[3];};
  34.     int i;
  35.     int dst[] = { 10, 20, 30 }, src[] = { 1, 2, 3 };
  36.  
  37.     *(struct T*)dst = *(struct T*)src;
  38.  
  39.     for (i = 0; i < 3; i++) printf("%dn", dst[i]);
  40.  
  41.     return 0;
  42. }
  43.  
  44. void fails(void) {
  45.     // will cause a compilation error, because struct T is an incomplete type.
  46.     struct T t;
  47. }
  48.        
  49. #include <stdio.h>
  50.  
  51. int main(void) {
  52.     // struct T is only visible in main()
  53.     struct T {int _[3];};
  54.     int i;
  55.     int dst[] = { 10, 20, 30 }, src[] = { 1, 2, 3 };
  56.  
  57.     *(struct T*)dst = *(struct T*)src;
  58.  
  59.     for (i = 0; i < 3; i++) printf("%dn", dst[i]);
  60.  
  61.     return 0;
  62. }
  63.  
  64. void fails(void) {
  65.     // will cause a compilation error, because struct T is an incomplete type.
  66.     struct T t;
  67. }