Pointers, arrays , functions

This is a discussion on Pointers, arrays , functions within the C Programming forums, part of the General Programming Boards category; Ok got a confusing assignment here. Instructions are to ask user for current date (month, day, year) and program calculates ...

  1. #1
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    Question Pointers, arrays , functions

    Ok got a confusing assignment here.

    Instructions are to ask user for current date (month, day, year) and program calculates time passed since Jan 1 2000. We are to assume 1) each year has 365 days 2) each month has 30 days.
    Limit to use of basic arrays and pointers. Just basic C stuff.

    First function prototype called from main:

    Code:
    void enter_datea(int *, int *, int *);
    // this function is to take information about current date entered on keyboard and information about month, day, year is passed back to main()

    My question here is why are pointers necessary???
    What are each pointer pointing to???


    Second funtion prototype called from main:

    Code:
    void time_elasped(int , int, int , int *, int *);
    //this function takes information about the current date and calculates the time elapsed since January 1, 2000. Time should be calculated in DAYS and HOURS and passed back to main()

    Now if you're only entering in months, days, years, how would you know the exact hour??? And how come three integers and 2 pointers passed???

    I would appreciate just a bit of a start to get me going here.
    Sue B.

    dazed and confused


  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Not given us too much to go on have you.... like some variable names to give us some idea of what you are passing into these functions....
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    now you know my confusion

    God knows a bit of this is not correct; but I am trying to understand the use of pointers as was mentioned in directions.
    (1) function enter_data seems to be where user inputs current date and should somehow realize pointers for month, for day, and for year

    (2) function time_elapsed seems to take the above info and calculate days first and then multiply that by 24 (hours in a day)

    Biggest problem I guess is getting the right variables labeled and which ones to use in the functions; so not sure about parameters in function (2).


    Code:
    #include <stdio.h>
    
    /*  define year to 365 days and month to 30 days */
    
    #define YEAR 365
    #define MONTH_DAYS 30
     
    /*  prototypes   */
    void enter_data(int *, int *, int *);
    void time_elapsed(int, int, int, int *, int*);
    
    main()
    {  
         int month, day, year;
         int *month, *day, *year;
         int *DAYS, *HOURS;
         printf("input current date as MM/DD/YY\n");
              
         enter_data(&month,&day,&year)
        
         time_elapsed(&month,&day,&year,DAYS,HOURS)
         
         printf("time elapsed since January 1, 2000\n" }; 
         printf("time elapsed in days", DAYS);
         printf("time elapsed in hours", HOURS);
    
    }
    
    void enter_data(int *month, int *day, int *year)
    
    {  
        scanf("%d %d %d", &month, &day, &year);
        year = year -2000;
        month = 
        day =
    }
    
    void time_elapsed(int month, int day, int year, int DAYS, int HOURS)
    
    {
          
          year *= 365;
          month *=30;
          DAYS = month+day+year;
          HOURS = DAYS *24;
    }
    Sue B.

    dazed and confused


  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,657
    Well you should give the pointers different names than the first int variables you declared. They are variables in and of themselves.

    Second, there is no reason not to give the arguments meaningful names in your prototypes. If nothing else to jog your memory every time you use it.

    >>>Now if you're only entering in months, days, years, how would you know the exact hour???

    Well you can't know that unless you ask the user for the current hour too. Otherwise, you can still give at least the number of hours elapsed not including those past 12 a.m. "this" morning for instance.

    Now I don't know why pointers or arrays would be necessary in this case, but that's teachers for you...Anyway, I have to go to bed, but if your still lost tommorrow I will take this up further



    ITSA
    Socket Library!

  5. #5
    Registered User Mangesh's Avatar
    Join Date
    Sep 2001
    Posts
    18

    Us e of pointers is necessary.

    Use of pointers in extremely important in your code. If you check the function enter_data(...), the values of year, month and day are being altered. We need the altered values in time_elapsed(...) function. Either way is to return a structure or make these variables as global. But your code is not using them as global variables and we have to stick to basic pointers and array.

    Hope I have clarified the point.

    Regards,
    Mangesh.

  6. #6
    Registered User Mangesh's Avatar
    Join Date
    Sep 2001
    Posts
    18

    solution for second query

    Sorry, I forgot to attend the second query.

    >> Now if you're only entering in months, days, years, how would you know the exact hour??? And how come three integers and 2 pointers passed???

    If you look into the main function, we need DAYS and HOURS only. which are the 4th and 5th parameters in the time_elapsed(...) function to display the elapsed time. We don't need first three parameters in the main function. So only the last 2 parameters are passed as pointers.

    Use of ctime etc. from time.h can give you the current time. It will help you to find out the hours also.

    Regards,
    Mangesh.

  7. #7
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    still confused

    i don't see why pointers are used in first function just to enter in the date.

    and what do the pointers point to?? what makes up the array they point to??

    Does the month pointer point to an element (say 12 elements in an array for the 12 months)?
    Does the day pointer point to an element (30 elements for the 30 days in a month)?
    Does the year pointer point to an element (2 years involved here 2000 and 2001)?
    Sue B.

    dazed and confused


  8. #8
    Unregistered
    Guest
    No. Unless YOU declare the array, the pointer does not point to an array.

    A pointer basically works like this:

    int num=5;
    int *pnum = &num; /// A pointer to "num"

    Now suppose that you knew what the address in memory that "num" is stored, and say that address is 1096. Suppose also you know the address of "pnum", and say that is 1216. The memory in your computer is tagged with adresses. Now when you assign the pointer to "num" you are saying: " pnum points to the address of num", so pnum points to 1096. So if you manipulate "num" directly, it may be assigned a new address, but if instead you manipulate "pnum", "pnum" may be assigned a new address, but "num" will stay put. No matter what you do with "pnum", even if it gets a new address, it will still always point to 1096. In this case,this has nothing to do with arrays.

    Now if you had declared "num" as an array of numbers:

    int num[5] = { 2, 4, 6, 8, 10 } ;
    int *pnum = &num[0]; // A pointer to the array "num"

    ...Then "pnum" would point to the first element in the array, "2".


    Now the alternate syntax for assigning a pointer to the first element in an array is:

    int *pnum = num;

    ...is the same as saying:

    int *pnum = &num[0];

    One more thing. In the case of strings, of course the pointer would be a pointer to an array, since words(strings) are arrays of chars. Also, you may assign a pointer to any element in an array:

    int *pnum = &num[1];

    ...would point to the second element, "4" in the array.

    I hope that helps.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,657
    No. Unless YOU declare the array, the pointer does not point to an array.

    A pointer basically works like this:

    int num=5;
    int *pnum = &num; /// A pointer to "num"

    Now suppose that you knew what the address in memory that "num" is stored, and say that address is 1096. Suppose also you know the address of "pnum", and say that is 1216. The memory in your computer is tagged with adresses. Now when you assign the pointer to "num" you are saying: " pnum points to the address of num", so pnum points to 1096. So if you manipulate "num" directly, it may be assigned a new address, but if instead you manipulate "pnum", "pnum" may be assigned a new address, but "num" will stay put. No matter what you do with "pnum", even if it gets a new address, it will still always point to 1096. In this case,this has nothing to do with arrays.

    Now if you had declared "num" as an array of numbers:

    int num[5] = { 2, 4, 6, 8, 10 } ;
    int *pnum = &num[0]; // A pointer to the array "num"

    ...Then "pnum" would point to the first element in the array, "2".


    Now the alternate syntax for assigning a pointer to the first element in an array is:

    int *pnum = num;

    ...is the same as saying:

    int *pnum = &num[0];

    One more thing. In the case of strings, of course the pointer would be a pointer to an array, since words(strings) are arrays of chars. Also, you may assign a pointer to any element in an array:

    int *pnum = &num[1];

    ...would point to the second element, "4" in the array.

    I hope that helps.

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    11

    Maybe...

    Why not use a different function for the subtraction of the entered date. Say date_substraction(int *, int *, int *);...

    -CDuddley

  11. #11
    Registered User Mangesh's Avatar
    Join Date
    Sep 2001
    Posts
    18

    Are you satisfied.

    Dear Sbellaw

    I suppose you are not yet satisfied. If I am correct please revert. I have confidence that you will be 100% satisfied. ARRAY DOESN'T COME INTO PICTURE. Please don't get confused. I will guide you, if you have not yet found the solution. I am 100% sure about my solution.

    Regards,
    Mangesh.

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,657
    Code:
    #include <stdio.h>
    #include <stdlib.h>
                       //__define year to 365 days and month to 30 days ___//
    #define YEAR_DAYS 365
    #define MONTH_DAYS 30
    
     int month = 0, day = 0, year = 0, DAYS = 0, HOURS = 0;
     int *pmonth = &month, *pday = &day, *pyear = &year, *PDAYS = &DAYS, *PHOURS = &HOURS;
    
                          //__prototypes__//
    
     void enter_date(int *pmonth, int *pday, int *pyear);
     void time_elapsed(int *pmonth, int *pday, int *pyear, int *PDAYS, int *PHOURS);
     void print_date_data( int DAYS, int HOURS );
    
    
                  //____Begin Main _______//
     int main()
    { 
    
    
         enter_date(&month, &day, &year);
         time_elapsed( &month, &day, &year, &DAYS, &HOURS);
         print_date_data( DAYS, HOURS);
    
         system("PAUSE");
         return 0;
    }
                  //___End Main___//
    
    
    
    
    
     void enter_date(int *pmonth, int *pday, int *pyear)
         {
         printf("Input current date as MM DD YY\n");
         printf("Please omit any other characters such as \"/\"...\n");
         scanf("%d %d %d", &month, &day, &year);
         }
    
    
    
     void time_elapsed(int *pmonth, int *pday, int *pyear, int *PDAYS, int *PHOURS)
         {
         DAYS = ( (year - 2000) * YEAR_DAYS ) + ( month * MONTH_DAYS ) + day;
         HOURS = DAYS * 24;
         }
    
    
    
       void print_date_data( int DAYS, int HOURS )
         {
         printf("Time Elapsed Since January 1, 2000...\n");
         printf("Time elapsed in days:  %i \n", DAYS);
         printf("Time elapsed in hours:  %i \n\n\n\n", HOURS);
         //printf("%d %d %d", month, day, year);
         }
    Enjoy!



    ITSA
    Socket Library!

  13. #13
    Registered User Mangesh's Avatar
    Join Date
    Sep 2001
    Posts
    18

    Attention Please

    Dear Sebastiani,

    time_elapsed(...) function doesn't have first three parameters as pointers and not need of these parameters as pointers, only last 2 parameters should be pointers.

    Regards,
    Mangesh.

  14. #14
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    I'm confused here!
    This is all about what happens to a variables contents when sent into a new function and on return to the calling function.
    It is trying to show the differnce between sending in &iNum or just iNum.

    Simple answer.
    You use a pointer if you want the actual value of a variable sent into a function to change.
    If you send in &iNum, the function can permanently change iNum.
    If you send in iNum then even if the function changes iNum it will revert back when the funtion ends.

    As far as I can see you do not need any variables apart from simple int. NO int * needed, NONE.

    The day month and year are declared as type int
    (in the MAIN())
    int iDay=0,iMonth=0,iYear=0;

    they are sent to the user for input as pointers so on return the value the user inputs will be returned

    InputFunct(&iDay,&iMonth,&iYear)

    if you send them as

    InputFunct(iDay,iMonth,iYear)
    they will not return with the new values but on return will revert back to the original value (declared as 0).

    In the function that works out the hours and days only the Hours and Days are pointers as only these two need to return new values

    The variables are all declared local to main reducing the need for (evil) globals and in the calc function they can not be changed by mistake

  15. #15
    Registered User Mangesh's Avatar
    Join Date
    Sep 2001
    Posts
    18
    I agree with novacain. One point is missing in his clarification. Pointers are needed to access those veriables in functions
    (enter_date(...)). This requires variables to be passed by address i.e. pointers. So understanding of pointers is necessary. The solution is literally simple.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 04:31 AM
  2. Problem with arrays, pointers and functions when combined
    By The Wazaa in forum C++ Programming
    Replies: 2
    Last Post: 02-05-2006, 09:44 AM
  3. Arrays and Functions
    By KunoNoOni in forum Game Programming
    Replies: 12
    Last Post: 10-04-2005, 09:41 PM
  4. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 12:48 PM
  5. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 09:33 PM

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