RicardasSim

return struct

Mar 7th, 2019
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct point{
  5. int x;
  6. int y;
  7. };
  8.  
  9. struct rect {
  10. struct point p1;
  11. struct point p2;
  12. };
  13.  
  14. /*
  15. typedef struct{
  16.     float x;
  17.     float y;
  18.     float z;
  19. }Vert;
  20. */
  21.  
  22. typedef struct vert{
  23.     float x;
  24.     float y;
  25.     float z;
  26. }Vert;
  27.  
  28. struct point testas1(int x, int y)
  29. {
  30.     struct point sP;
  31.  
  32.     sP.x = x;
  33.     sP.y = y;
  34.    
  35.     return sP;
  36. }
  37.  
  38. struct rect testas2()
  39. {
  40.     struct rect sR;
  41.  
  42.     sR.p1.x = 1;
  43.     sR.p1.y = 2;
  44.    
  45.     struct point ptl;
  46.    
  47.     ptl.x=3;
  48.     ptl.y=4;
  49.    
  50.     sR.p2 = ptl;
  51.    
  52.     return sR;
  53. }
  54.  
  55. Vert testas3(float x, float y, float z)
  56. {
  57.     Vert sV;
  58.  
  59.     sV.x = x;
  60.     sV.y = y;
  61.     sV.z = z;
  62.    
  63.     return sV;
  64. }
  65.  
  66. int main (int argc, char **argv){
  67.  
  68.     struct point pt;
  69.    
  70.     struct rect fgr;
  71.    
  72.     Vert sVrt;
  73.    
  74.     Vert* pVrt;
  75.    
  76.     pt = testas1(1, 2);
  77.    
  78.     printf("%d %d\n", pt.x, pt.y);
  79.  
  80.     fgr = testas2();
  81.    
  82.     printf("%d %d %d %d\n", fgr.p1.x, fgr.p1.y, fgr.p2.x, fgr.p2.y);
  83.    
  84.     sVrt = testas3( 0.1 , 0.2 , 0.3 );
  85.    
  86.     printf("%f %f %f\n", sVrt.x, sVrt.y, sVrt.z);
  87.    
  88.     printf("sizeof(float):\t%zu\n", sizeof(float));
  89.     printf("sizeof(Vert):\t%zu\n", sizeof(Vert));
  90.    
  91.     pVrt = malloc(sizeof(Vert));
  92.    
  93.     if(pVrt != NULL)
  94.     {
  95.    
  96.         *pVrt = testas3( 0.12 , 0.22 , 0.32 );
  97.        
  98.         printf("%f %f %f\n", pVrt->x, pVrt->y, pVrt->z);
  99.        
  100.         free(pVrt);
  101.     }
  102.     else printf("memory allocation error.\n");
  103.    
  104. return 0;
  105. }
  106.  
  107. /*
  108. output:
  109.  
  110. 1 2
  111. 1 2 3 4
  112. 0.100000 0.200000 0.300000
  113. sizeof(float):  4
  114. sizeof(Vert):   12
  115. 0.120000 0.220000 0.320000
  116.  
  117. */
Advertisement
Add Comment
Please, Sign In to add comment