Thread: passing structure arguments to function

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123

    passing structure arguments to function

    Code:
    // EX_3.c  CHAPTER 8
    
    
    #include <stdio.h>
    
    
    
    
    struct time1
    {
    	int hour;
    	int minute;
    	int second;
    };
    
    
    struct time2
    {
    	int hour;
    	int minute;
    	int second;
    };
    
    
    struct time3
    {
    	int hour;
    	int minute;
    	int second;
    };
    
    
    int main (void)
    {
    	struct time3 time_calculate_function (struct time1 link_1, struct time2 link_2);
    	struct time1 one[3] = { { 3, 45, 15 } }; // link_1
    	struct time2 two[3] = { { 9, 44, 3 } }; // link_2
    	struct time3 three[3];
    	
    	int i;
    
    
    	for(i = 0; i < 1; ++i)
    	{
    		printf("%i:%i:%i\n", one[i].hour, one[i].minute, one[i].second);
    		printf("%i:%i:%i\n", two[i].hour, two[i].minute, two[i].second);
    		printf("Difference in time: \n");
    		three[i] = time_calculate_function (one[i], two[i]);
     		printf("%i:%i:%i\n", three[i].hour, three[i].minute, three[i].second);
    	}
    
    
    	return 0;
    }
    
    
    struct time3 time_calculate_function (struct time1 link_1, struct time2 link_2)
    {
    	// return difference 5:58:48 seconds
    
    
    	struct time3 foo;
    
    
    	return foo;
    }
    quick question, i'm trying to get the fourth printf function call to print "5:58:48" seconds and I will be doing the code inside the time_calculate_function which will be returning a structure. Also, i have a question about passing a structure array

    Code:
    (one[i], two[i]);

    I am looking for the answer (your reply) how to have the structure's time1 and time2 passed to the time_calculate_function so I can access each member inside the function for example one.hour, two.hour etc. also as stated in the book this function (time_calculate_function) has to return a structure and I understand the link_1 and link_2 are the references passed from the function but how to reference the structure time3.

    The question in the programming book is:

    CH 8 EX #3.) Write a function elapsed_time that takes as its arguments two time structures and returns a time structure that represents the elapsed time (in hours, minutes, and seconds) between the two times. So the call


    elapsed_time (time1, time2)


    where time1 represents 3:45:15 and time2 represents 9:44:03, should return a time structure that represents 5 hours, 58 minutes, and 48 seconds. Be careful with times that cross midnight

    Thanks all!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think when the requirements ask for you to pass multiple time structs, they mean multiple objects (or pointers thereof) of the same time struct type, not three time struct types that are identical except for name.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing arguments to a function
    By cfanatic in forum C Programming
    Replies: 2
    Last Post: 07-01-2012, 06:50 AM
  2. Help passing arguments to function
    By HAssan in forum C Programming
    Replies: 2
    Last Post: 11-26-2007, 02:15 PM
  3. Passing arguments to another function
    By Wiretron in forum C Programming
    Replies: 2
    Last Post: 12-24-2006, 05:57 AM
  4. need function help, passing arguments
    By infernosnow in forum Game Programming
    Replies: 18
    Last Post: 07-18-2006, 02:45 AM
  5. Passing arguments to function...
    By alvifarooq in forum C++ Programming
    Replies: 8
    Last Post: 09-24-2004, 12:50 PM

Tags for this Thread