dmilicev

lets_learn_structures_in_c_v1.c

Nov 8th, 2019
281
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.09 KB | None
  1. /*
  2.  
  3.     lets_learn_structures_in_c_v1.c
  4.  
  5.  
  6.     Example of work with structures in C.
  7.  
  8.  
  9.     https://www.tutorialspoint.com/cprogramming/c_structures.htm
  10.  
  11.     https://www.geeksforgeeks.org/structures-c/
  12.  
  13.     https://www.learn-c.org/en/Function_arguments_by_reference
  14.  
  15.  
  16.     https://www.programiz.com/c-programming/c-structures
  17.  
  18.     https://www.programiz.com/c-programming/c-structures-pointers
  19.  
  20.     https://www.programiz.com/c-programming/c-structure-function
  21.  
  22.  
  23.     https://fresh2refresh.com/c-programming/c-structures/
  24.  
  25.     https://fresh2refresh.com/c-programming/c-structure-using-pointer/
  26.  
  27.     https://fresh2refresh.com/c-programming/c-nested-structure/
  28.  
  29.  
  30.     You can find all my C programs at Dragan Milicev's pastebin:
  31.  
  32.     https://pastebin.com/u/dmilicev
  33.  
  34.     https://www.facebook.com/dmilicev
  35.  
  36. */
  37.  
  38. #include <stdio.h>
  39. #include <string.h>
  40.  
  41. #define LEFT        75      // left arrow
  42. #define TP_NO       0       // default value, there is no contact of the rectangle with the wall
  43.  
  44. struct Rectangle {
  45.     int num;
  46.     float fnum;
  47.     char ch;
  48.     char str[100];
  49. };
  50.  
  51. // the function receives the value of the structure and therefore cannot modify its elements
  52. int function_arguments_by_value( struct Rectangle R )
  53. {
  54.  
  55.     printf("\n before change in function_arguments_by_value \n");
  56.     printf("\n R.num = %d \n", R.num );
  57.     printf("\n R.fnum = %.2f \n", R.fnum );
  58.     printf("\n R.ch = %c \n", R.ch );
  59.     printf("\n R.str = %s \n\n", R.str );
  60.  
  61.     R.num = 456;            // let's change values
  62.     R.fnum = 56.78;
  63.     R.ch = 'B';
  64.     strcpy( R.str, "This is new string from function_arguments_by_value()." );
  65.  
  66.     printf("\n after change in function_arguments_by_value() \n");
  67.     printf("\n R.num = %d \n", R.num );
  68.     printf("\n R.fnum = %.2f \n", R.fnum );
  69.     printf("\n R.ch = %c \n", R.ch );
  70.     printf("\n R.str = %s \n\n", R.str );
  71.  
  72.     return 0;
  73. }
  74.  
  75. // the function gets the address of the structure and therefore can modify its elements
  76. int function_arguments_by_reference( struct Rectangle *R )
  77. {
  78.  
  79.     printf("\n before change in function_arguments_by_reference() \n", (*R).num );
  80.     //printf("\n R.num = %d \n", (*R).num );
  81.     // or, we have a shorthand syntax for that:
  82.     printf("\n R.num = %d \n", R->num );
  83.     printf("\n R.fnum = %.2f \n", R->fnum );
  84.     printf("\n R.ch = %c \n", R->ch );
  85.     printf("\n R.str = %s \n\n", R->str );
  86.  
  87.     //(*R).num = 768;
  88.  
  89.     // or, we have a shorthand syntax for that:
  90.  
  91.     R->num = 768;           // let's change values
  92.     R->fnum = 56.78;
  93.     R->ch = 'B';
  94.     strcpy( R->str, "This is new string from function_arguments_by_reference()." );
  95.  
  96.     //printf("\n after change in function_arguments_by_reference() R.num = %d \n", (*R).num );
  97.  
  98.     // or, we have a shorthand syntax for that:
  99.  
  100.     printf("\n after change in function_arguments_by_reference() \n");
  101.     printf("\n R.num = %d \n", R->num );
  102.     printf("\n R.fnum = %.2f \n", R->fnum );
  103.     printf("\n R.ch = %c \n", R->ch );
  104.     printf("\n R.str = %s \n\n", R->str );
  105.  
  106.     return 0;
  107. }
  108.  
  109.  
  110. /*
  111. Reminder:
  112.  
  113.     struct Rectangle {
  114.         int num;
  115.         float fnum;
  116.         char ch;
  117.         char str[100];
  118.     };
  119. */
  120. int main()
  121. {
  122.     struct Rectangle R;
  123.  
  124.     R.num = 123;                        // initial values
  125.     R.fnum = 12.34;
  126.     R.ch = 'A';
  127.     strcpy( R.str, "This is string in main()" );
  128.  
  129.     printf("\n main() before function_arguments_by_value() \n");
  130.     printf("\n R.num = %d \n", R.num );
  131.     printf("\n R.fnum = %.2f \n", R.fnum );
  132.     printf("\n R.ch = %c \n", R.ch );
  133.     printf("\n R.str = %s \n\n", R.str );
  134.  
  135.  
  136.     function_arguments_by_value( R );
  137.  
  138.     printf("\n main() after function_arguments_by_value() \n");
  139.     printf("\n R.num = %d \n", R.num );
  140.     printf("\n R.fnum = %.2f \n", R.fnum );
  141.     printf("\n R.ch = %c \n", R.ch );
  142.     printf("\n R.str = %s \n\n", R.str );
  143.  
  144.  
  145.     printf("\n\n Notice that nothing was changed in main() \n after function_arguments_by_value() \n\n");
  146.  
  147.     function_arguments_by_reference( &R );
  148.  
  149.     printf("\n main() after function_arguments_by_reference() \n");
  150.     printf("\n R.num = %d \n", R.num );
  151.     printf("\n R.fnum = %.2f \n", R.fnum );
  152.     printf("\n R.ch = %c \n", R.ch );
  153.     printf("\n R.str = %s \n\n", R.str );
  154.  
  155.     printf("\n\n Notice that everything was changed in main() \n after function_arguments_by_reference() \n\n");
  156.  
  157.  
  158.     return 0;
  159. }
RAW Paste Data Copied