Advertisement
dllbridge

Untitled

Jul 12th, 2023 (edited)
1,122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.59 KB | None | 0 0
  1.  
  2.  
  3. #include   <stdio.h>
  4.  
  5.  
  6.  
  7.  
  8.  
  9. ////////////////////////////////////////////////////
  10. struct Cday
  11. {
  12.    
  13.     int          nYear;
  14.    
  15.     const char *pszDay;
  16.    
  17.     int           nSec;
  18.    
  19.    
  20. };
  21.  
  22.  
  23. void foo(Cday d);
  24.  
  25.  
  26. ////////////////////////////////////////////////////
  27. int main()                                        //
  28. {
  29.    
  30.     Cday day1;
  31.    
  32.     day1.nYear =      2022;
  33.    
  34.     day1.pszDay = "Monday";
  35.    
  36.     day1.nSec = 1688169600;
  37.    
  38.     printf("size Cday = %d\n", sizeof(day1) );
  39.     printf("address day1 = %d\n", &day1);
  40.     foo(day1);
  41.  
  42. }
  43.  
  44.  
  45.  
  46. ////////////////////////////////////////////////
  47. void foo(Cday d)
  48. {
  49.      printf("address  d   = %d\n", &d);
  50.      
  51.      printf("d.nYear   = %d\n", d.nYear );
  52.      printf("d.pszDay  = %s\n", d.pszDay);
  53.      printf("d.nSec    = %d\n", d.nSec  ); 
  54.      printf(" - - - - - - -\n"    );
  55.      printf("d.nYear   = %d\n", d );
  56. //   printf("d.pszDay  = %s\n", d  );
  57.      printf("d.nSec    = %d\n", (int)(&d)+8  );      
  58.      
  59. }
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98. ///  Задачка получилась сложная.  Вот моё решение:
  99. //   Если сможете упростить, будет отлично.
  100.  
  101.  
  102.  
  103. #include   <stdio.h>
  104.  
  105.  
  106.  
  107.  
  108.  
  109. ////////////////////////////////////////////////////
  110. struct Cday
  111. {
  112.    
  113.     int                nYear;
  114.     const char       *pszDay;
  115.     int                 nSec;  
  116. };
  117.  
  118.  
  119. void foo(Cday d);
  120.  
  121.  
  122. ////////////////////////////////////////////////////
  123. int main()                                        //
  124. {
  125.    
  126.     Cday day1;
  127.    
  128.     day1.nYear =      2022;
  129.    
  130.     day1.pszDay = "Monday";
  131.    
  132.     day1.nSec = 1688169600;
  133.    
  134.     printf("size Cday = %d\n", sizeof(day1) );
  135.     printf("address day1 = %d\n", &day1);
  136.     foo(day1);
  137.  
  138. }
  139.  
  140.  
  141.  
  142. ////////////////////////////////////////////////
  143. void foo(Cday d)
  144. {
  145.      printf("address  d   = %d\n", &d);
  146.      
  147.      printf("d.nYear   = %d\n", d.nYear );
  148.      printf("d.pszDay  = %s\n", d.pszDay);
  149.      printf("d.nSec    = %d\n", d.nSec  ); 
  150.      printf(" - - - - - - -\n"    );
  151.      printf("d.nYear   = %d\n", d );
  152. //   printf("d.pszDay  = %s\n", d  );
  153.  
  154.  
  155. char *p = (char*)&d;
  156.  
  157.       p = p + 4;
  158.      
  159.    
  160.  
  161.                             printf("d.pszDay address = %d\n",   p  );
  162.                            
  163.       int *pn  = (int*)p;                    
  164.      
  165.       char *psz2 = (char*)*pn;
  166.                             printf("d.pszDay         = %s\n",  psz2  );
  167.    
  168.       p = p + 4;
  169.      
  170.                             printf("d.nSec address   = %d\n",   p  );                  
  171.       pn = (int*)p;         printf("d.nSec           = %d\n",  *pn );        
  172.      
  173. }
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement