Using pointers

This is a discussion on Using pointers within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> void find_closest_flight(int desired_time, int *departure_time, int *arrival_time); int main() { int hour = 0; int ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    2

    Using pointers

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void find_closest_flight(int desired_time, int *departure_time, int *arrival_time);
    
    
    
    
    int main()
    {
        int hour = 0;
        int minute = 0;
    
        // Departure times in minutes after midnight
        // int departure_times[] = 
        // {8*60, 9*60+43, 11*60+19, 12*60+47, 
        // 14*60, 15*60+45, 19*60, 21*60+45};
    
        // Arrival times in minutes after midnight
        // int arrival_times[] =
        // { 10*60+16, 11*60+52, 13*60+31, 15*60, 
        // 16*60+8, 17*60+55, 21*60+20, 23*60+58};
    
        // Length of the array.
        //int length = sizeof(arrival_times) / sizeof(arrival_times[0]);
    
        int departure_hour, departure_min;
        char departure_halfday;
        int arrival_hour, arrival_min;
        char arrival_halfday;
    
        // int i;
        // int time_in_minutes;
        // int difference1, difference2, difference3;
        // int min_difference, best_index;
    
        printf ("\nEnter a 24-hour time: ");
        scanf("%d:%d", &hour, &minute);
        printf ("You entered %2d:%02d\n", hour, minute);
    
        // Get user's time in minutes after midnight.
        //time_in_minutes = hour*60 + minute;
    
         //Look through the departure time array and find the value that
         //is closest to the time entered by the user.
        //min_difference = 100000;
        // for (i = 0; i < length; i++)
        // {
            //Difference between user's time and flight time today.
            // difference1 = abs(time_in_minutes - departure_times[i]);
            // if (difference1 < min_difference)
            // {
                // min_difference = difference1;
                // best_index = i;
            // }
    
           // Difference between user's time and flight time previous day.
            // difference2 = abs(time_in_minutes - (departure_times[i]  - 60*24));
            // if (difference2 < min_difference)
            // {
                // min_difference = difference2;
                // best_index = i;
            // }
    
            //Difference between user's time and flight time next day.
            // difference3 = abs(time_in_minutes - (departure_times[i] + 60*24));
            // if (difference3 < min_difference)
            // {
                // min_difference = difference3;
                // best_index = i;
            // }
        // }
    
        // We have determined the closes departure time in minutes after midnight.
        // Now output it as a time of day.
        // i = best_index;
        // departure_hour = departure_times[i] / 60;
        // departure_min = departure_times[i] % 60;
        // departure_halfday = 'a';
        // arrival_hour = arrival_times[i] / 60;
        // arrival_min = arrival_times[i] % 60;
        // arrival_halfday = 'a';
    
        if (departure_hour >= 12)
        {
            // Departure is at noon or later.
            departure_halfday = 'p';
            if (departure_hour > 12)
            {
                departure_hour -= 12;
            }
        }
        else
        {
           // Departure is before noon.
           if (departure_hour < 1)
            {
                // 30 minutes after midnight should be 12:30 A.M.
                departure_hour += 12;
            }
        }
    
        if (arrival_hour >= 12)
        {
            // Arrival time is at noon or later.
            arrival_halfday = 'p';
            if (arrival_hour > 12)
            {
                arrival_hour -= 12;
            }
        }
        else
        {
           // Arrival time is before noon.
           if (arrival_hour < 1)
            {
                arrival_hour += 12;
            }
        }
    
        printf ("closest departure time is %2d:%02d %c.m., ",
            departure_hour, departure_min, departure_halfday);
    
        printf("arriving at %2d:%02d %c.m.",
            arrival_hour,   arrival_min,   arrival_halfday);
        getchar();
        getchar();
        return 0;
    }
    
    
    void find_closest_flight(int desired_time, int *departure_time, int *arrival_time)
    {
    	int hour = 0;
    	int minute = 0;
    	//int desired_time;
    	//int departure_time;
    	//int arrival_time;
    	
    	departure_time = &departure_time;
    	arrival_time = &arrival_time;
    	
    	int departure_hour, departure_min;
        char departure_halfday;
        int arrival_hour, arrival_min;
        char arrival_halfday;
    	
    	
        int i;
        int time_in_minutes;
        int difference1, difference2, difference3;
        int min_difference, best_index;
    	
    	 // Departure times in minutes after midnight
        int departure_times[] = 
        {8*60, 9*60+43, 11*60+19, 12*60+47, 
        14*60, 15*60+45, 19*60, 21*60+45};
    
        // Arrival times in minutes after midnight
        int arrival_times[] =
        { 10*60+16, 11*60+52, 13*60+31, 15*60, 
        16*60+8, 17*60+55, 21*60+20, 23*60+58};
    	
    	i = best_index;
        departure_hour = departure_times[i] / 60;
        departure_min = departure_times[i] % 60;
        departure_halfday = 'a';
        arrival_hour = arrival_times[i] / 60;
        arrival_min = arrival_times[i] % 60;
        arrival_halfday = 'a';
    	int length = sizeof(arrival_times) / sizeof(arrival_times[0]);
    		
        // Get user's time in minutes after midnight.
        time_in_minutes = hour*60 + minute;	
    	
    	//Look through the departure time array and find the value that
         //is closest to the time entered by the user.
        min_difference = 100000;
        for (i = 0; i < length; i++)
        {
            // Difference between user's time and flight time today.
            difference1 = abs(time_in_minutes - departure_times[i]);
            if (difference1 < min_difference)
            {
                min_difference = difference1;
                best_index = i;
            }
    
            // Difference between user's time and flight time previous day.
            difference2 = abs(time_in_minutes - (departure_times[i]  - 60*24));
            if (difference2 < min_difference)
            {
                min_difference = difference2;
                best_index = i;
            }
    
            // Difference between user's time and flight time next day.
            difference3 = abs(time_in_minutes - (departure_times[i] + 60*24));
            if (difference3 < min_difference)
            {
                min_difference = difference3;
                best_index = i;
            }
        
    return;
    		}

    Does anyone know how to write pointers in the (void) Function. Also how store time inside the pointers.
    Attached Files Attached Files

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What do you mean by "write pointers in the function"? If intptr is a pointer-to-int, you can store an int at the address pointed to by writing *intptr = 43.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    2
    I mean declaring and calling a pointer under the void find_closest_flight(int desired_time, int *departure_time, int *arrival_time) seen in my example.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You declare a pointer the same as any other variable: type first, then name. You cannot call a pointer, just as you cannot call any other variable. Edit: And this
    Code:
    	departure_time = &departure_time;
    	arrival_time = &arrival_time;
    is a really bad idea.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 06:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM

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