Passing structures to a function

This is a discussion on Passing structures to a function within the C Programming forums, part of the General Programming Boards category; Hello all, there are two main problems I am having with this program. The first is that I am not ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    43

    Passing structures to a function

    Hello all, there are two main problems I am having with this program. The first is that I am not sure how to pass the two structure variables (date tom, and date mom) into the function and then have it execute. Also my series of do-while statements and if statements when executed on their own print infinitely, they do print the correct answer just far too many. Help would be great thank you

    Code:
    #include <stdio.h>
    
    struct date
    {
       int month;
       int day;
       int year;
    };
    
    
    void larger (struct date, struct date);
    
    
    int main()
    {
    
    
    struct date tom;
    struct date mom;
    
    
    puts("Enter a month, day, and year separated by spaces (mm dd yy):\n");
    scanf("%d %d %d", &tom.month, &tom.day, &tom.year);
    
    
    puts("Enter another month, day, and year separated by spaces (mm dd yy):\n");
    scanf("%d %d %d", &mom.month, &mom.day, &mom.year);
    
    
    larger (struct date tom, struct date mom);
        
    system ("PAUSE");
    return 0;
    }
        
    void larger (struct date tom, struct date mom){
    
    
        if (tom.year>mom.year)
        printf("\nThe larger date is: %d/%d/%d \n\n",tom.month,tom.day,tom.year % 100);
        
        else if (mom.year>tom.year)
        printf("\nThe larger date is: %d/%d/%d \n\n",mom.month,mom.day,mom.year % 100);
        
        do{       
               if (tom.month>mom.month)
               printf("\nThe larger date is: %d/%d/%d \n\n",tom.month,tom.day,tom.year % 100);
        
               else if (mom.month>tom.month)
               printf("\nThe larger date is: %d/%d/%d \n\n",mom.month,mom.day,mom.year % 100);
               do{
                   if (tom.day>mom.day)
                   printf("\nThe larger date is: %d/%d/%d \n\n",tom.month,tom.day,tom.year % 100);
        
                   else if (mom.day>tom.day)
                   printf("\nThe larger date is: %d/%d/%d \n\n",mom.month,mom.day,mom.year % 100);  
                   do{
                       printf ("\nThe two dates are the same.\n\n");
                     } while (tom.day==mom.day); 
                      
                   }   while (tom.month==mom.month);
                   
          }while (tom.year==mom.year);
    }

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    593
    Look at your do while loops, when will tom.whatever it may be != mom.whatever it may be. You need to increment those variables in there or else it will be possible infinite loops.

    I have not ran your code but you might want to change your while condition to != instead of ==. That is just from what I can see right now, I might not be seeing it right.

    EDIT:
    Also, when you call your function in main you do not need the data type, just use the name. Drop the struct date part when you call it larger in main.

    ex)
    Code:
    larger (struct date tom, struct date mom);
    to

    Code:
    larger (tom,mom);
    Last edited by camel-man; 12-04-2012 at 08:08 PM.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    Thanks for your help calling the function, I am however not sure what you mean about the incrementation of the variables in the loops. Sorry I just have been trying to get this to go correctly and I'm not too sure how to change it.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,234
    Quote Originally Posted by Matticus View Post
    For instance, look at some of your "while()" loops. They will clearly run forever.

    Code:
    while(condition)
    {
        /* if "condition" is true, then the code in this body will execute */
    
        /* however, if "condition" is never updated in this loop,
           it will always be true and the loop will go on forever */
    }
    The same clearly holds true for "do-while()" loops.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    Awesome thank you, I think earlier my brain was all jumbled up and I couldn't process the guidance. So thank you Matticus for the great guidance and thanks camel-man for the help with the function

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,294
    What makes you think it would be appropriate for that function to have any loops in it?

    How about just returning a value such as -1, 0, or 1 and then do the printing from main? It would save having 6 copies of the same printf statement.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    333
    Most dialects of C won't allow you to pass structures to functions.

    You need to write

    Code:
    void larger(struct date *tom, struct date *mom)
    {
      if(tome->year > mom->year)
         etc
    }
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Malcolm McLean View Post
    Most dialects of C won't allow you to pass structures to functions.
    What's a "dialect of C"?

    In standard/ANSI/ISO C there is no problem passing structs as arguments and returning them.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Structures as Function Arguments
    By Jyqft in forum C Programming
    Replies: 13
    Last Post: 03-26-2012, 08:02 AM
  2. Passing An Array Of Structures To A Function
    By nexusdarkblue in forum C Programming
    Replies: 3
    Last Post: 03-10-2009, 07:24 AM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. Passing structures to a function
    By earth_angel in forum C++ Programming
    Replies: 5
    Last Post: 07-13-2005, 06:13 AM
  5. passing array of structures to function
    By bvnorth in forum C Programming
    Replies: 3
    Last Post: 08-22-2003, 07:15 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21