Advertisement
Guest User

structs.c

a guest
Nov 3rd, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.40 KB | None | 0 0
  1. /* This is a very commented piece of code so beginners at C/C++ can
  2.  * hopefully understand how they work :) */
  3.  
  4. #include <stdio.h>
  5.  
  6. /* the struct keyword is almost like typedef.  You make the base struct and give
  7.  * it a name, then you actually use that struct to create others that inherrit
  8.  * the same variables that are inside of it.  Here we make a structure called
  9.  * "time", that contains the hour, minute, and seconds... it holds the info for
  10.  * all of the other structs we will declare that is of the same type. */
  11. struct time
  12. {
  13.     int hour;
  14.     int minutes;
  15.     int seconds;
  16. };
  17.  
  18.  
  19. int main(void)
  20. {
  21.    
  22.     /* prototype for the function timeUpdate.  It will return a struct */
  23.     struct time timeUpdate(struct time now);
  24.    
  25.    
  26.     /* This is declaring that the structs currentTime and nextTime are of
  27.      * the type "time", so they inherit all of the qualities of the
  28.      * time struct */
  29.     struct time currentTime, nextTime;
  30.    
  31.     /* Get the current time and put it into the struct "currentTime" */
  32.     printf("Enter the time (hh:mm:ss): ");
  33.     scanf("%i:%i:%i", &currentTime.hour,
  34.             &currentTime.minutes, &currentTime.seconds);
  35.    
  36.     /* Run a function that will update the time, passing it
  37.      * the current time, and the function will return a structure...
  38.      * that will equal "nextTime".  Makes sense if you think about it
  39.      * remember that "nextTime" has been declared on line 22 and it has
  40.      * all of the qualities of the global struct "time" that was declared
  41.      * at the very beginning of the code. */
  42.     nextTime = timeUpdate(currentTime);
  43.    
  44.     /* Of course, this is just printing out the nextTime. */
  45.     printf("Updated time is %.2i:%.2i:%.2i\n", nextTime.hour,
  46.                 nextTime.minutes, nextTime.seconds);
  47.    
  48.     return 0;
  49. }
  50.  
  51. /* function to update the time by one second and the structure that is
  52.  * passed to the function timeUpdate is reffered to as "now" */
  53.  
  54. struct time timeUpdate (struct time now)
  55. {
  56.     ++now.seconds;
  57.     if(now.seconds == 60){
  58.         now.seconds = 0;
  59.         ++now.minutes;
  60.        
  61.         if(now.minutes == 60){
  62.             now.minutes = 0;
  63.             ++now.hour;
  64.            
  65.             if(now.hour == 24)
  66.                 now.hour = 0;
  67.         }
  68.     }
  69.    
  70.     /* You can see that this function is of the type "struct time", so it is
  71.      * going to RETURN a structure that holds the same contents of time...
  72.      * of course, the struct "now" has been changed by all of the if statements
  73.      * above. (this is what makes one more second on the "clock" so to speak */
  74.     return now;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement