Thread: problems with pointers

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    11

    problems with pointers

    Hi everyone, I need help with the pointers. Here's my question:

    Using a pointer, write a function named time() that accepts an integer number of seconds and the addresses of three variables named hours, min, nad sec. The function is to convert the passed number of seconds into an equivalent number of hours, minutes, and seconds and directly alter the vlaue of respective variables using the passed addresses.

    To test, run the program using 10,000 seconds.

    This is what I have done so far, but its not working...

    Code:
    #include<stdio.h>
    
    int time(int time, int *hours, int *mins, int *secs);
    
    int main()
    
    {
    
    int time, secs, mins, hours;
    
    printf("Enter the number of time in seconds: ");
    scanf("%d", &time);
    printf("\nHours: ", &hours);
    printf("\nMinutes: ", &mins);
    printf("\nSeconds: ", &secs);
    
    return 0;
    }
    
    int time (int time, int *hours, int *mins, int *secs)
    {
    
    	*hours = time / 3600;
    	time %= 3600;
    
    	*mins = time / 60;
    	time %= 60;
    
    	*secs = time;
    
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2003
    Location
    Austria
    Posts
    55
    You have to call your function after the "scanf-line" - thats the main point, why it's not working .
    The call should look like this: time(time, &hours, &mins...
    "&hours" represents the adress of the "hours" variable - so you can use it as a pointer in your function.

    The output of your values is also incorrect a bit - it schould look like this:
    printf("\nHours: %d", hours);

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Aside from trying to redefine the time() function which is very likely a function recognized by your compiler already, there are problems here:
    Code:
    int main()
    
    {
    
    int time, secs, mins, hours;
    
    printf("Enter the number of time in seconds: ");
    scanf("%d", &time);
    printf("\nHours: ", &hours);
    printf("\nMinutes: ", &mins);
    printf("\nSeconds: ", &secs);
    
    return 0;
    }
    1) You define an int time. You can't have more than one symbol with the same name (e.g. a veriable and a function.)
    2) You don't actually ever call your time() function.
    3) Your printf()'s are all missing format specifiers. Also, you don't want to print the address of those variables, you want to print the value of those variables.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    Using a pointer, write a function named time() that accepts an integer number of seconds and the addresses of three variables named hours, min, nad sec. The function is to convert the passed number of seconds into an equivalent number of hours, minutes, and seconds and directly alter the vlaue of respective variables using the passed addresses.
    obviously, your teacher does not have much experience with C or the book you are using is really stupid..because, if you decide to include time.h, you will have problems if you are working with C . with C++, there wont be any problem as such, but its not a good programming practice..

    now lets take a look at your code.
    Code:
    int time(int time, int *hours, int *mins, int *secs);
    
    int main()
    
    {
    
    int time, secs, mins, hours;
    your fuction and a variable have the same name. that wont do. you have to change one of them or rather both of them and name them differently. try naming the function as time1 and the variable as time2


    Code:
    printf("\nHours: ", &hours);
    printf("\nMinutes: ", &mins);
    printf("\nSeconds: ", &secs);
    printf() doesnt work that way. you need format specifiers to display some variable value..also, you want to print the values. when you try to print the value of &hours, the address of the variable hours is printed, and not the value in it.
    you will find more info about format specifiers here..http://www.austin.cc.tx.us/rickster/...uts/printf.htm

    check the tutorials on the site as well.

    lastly, you need to cal lthe function in the main program. you cant just define and declare a function and expect it to work that way . again, check the tutorials on the site for working with functions..
    Last edited by PING; 03-29-2005 at 12:45 PM.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    11

    :s

    What do you mean?

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Mahesh
    What do you mean?
    About what? What are you confused about?
    If you understand what you're doing, you're not learning anything.

  7. #7
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    My guess would be the address and value thingie. Here. Let me draw you a little picture.
    Code:
    -------------------------------------------------------------------
    /**********/**********/**********/**********/**********/**********/  //variable name
    |    20    |    21    |    22    |    23    |    24    |    25    |  //address
    |          |          |          |          |          |          |  //binary
    /**********/**********/**********/**********/**********/**********/  //value
    -------------------------------------------------------------------
    This is a little figurative map of a small section of computer memory. Each of those sections is a byte, consitsting of eight bits. To distinguish between each byte, they are each given their own address. As you can see, those bytes have the addresses of 20 through 25. Now, here's what happend if I declared a couple of variables.
    Code:
    int a = 0;
    int b = 5;
    int c = 10;
    Code:
    -------------------------------------------------------------------
    /**** a ***/**** b ***/**** c ***/**********/**********/**********/  //variable name
    |    20    |    21    |    22    |    23    |    24    |    25    |  //address
    | 00000000 | 00000101 | 00001010 |          |          |          |  //binary
    /**** 0 ***/**** 5 ***/*** 10 ***/**********/**********/**********/  //value
    -------------------------------------------------------------------
    When the code is compiled, the compiler assigns each variable to a certain byte. (note that the variable name is not actually stored in the byte) Now when you put the address operator (&) in front of a variable, it outputs the address of the variable, not the value stored there. Like so.
    Code:
    main()
     {
      printf("a = %i\n", a);
      printf("&a = %i\n", &a);
     }
    If the variables were assigned as above, my output would be.
    Code:
    a = 0
    &a = 20
    Think you got it?

  8. #8
    Registered User Dave Jay's Avatar
    Join Date
    Mar 2002
    Posts
    33

    I'd do something like this if I read you correctly:

    Code:
    /* Don't name the executable "time.exe", as  this is a conflicting
       name in some environments
    */
    
    #include <stdio.h>
    #define HOURSEC 3600
    #define MINSEC 60
    
    void my_time(const unsigned, unsigned*, unsigned*, unsigned*);
    
    int main()
    {
      unsigned time_in_secs, hours, mins, secs;
    
      printf("Enter a time in seconds: ");
      scanf("%d", &time_in_secs);
    
      my_time(time_in_secs, &hours, &mins, &secs);
    
      printf("  hours = %d\nminutes = %d\nseconds = %d\n", hours, mins, secs);
    
      return 0;
    }
    
    void my_time (const unsigned t, unsigned *h, unsigned *m, unsigned *s)
    {
      *h = t / HOURSEC;
      *m = (t - HOURSEC * *h) / MINSEC;
      *s = t % MINSEC;
    }
    Last edited by Dave Jay; 03-29-2005 at 08:19 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. problems passing pointers
    By doormat in forum C Programming
    Replies: 9
    Last Post: 04-11-2004, 04:38 PM
  3. Class Pointers
    By ventolin in forum C++ Programming
    Replies: 8
    Last Post: 04-04-2004, 06:07 PM
  4. Array of Pointers + Deleting An Object = Problems
    By Nereus in forum C++ Programming
    Replies: 3
    Last Post: 03-04-2004, 12:16 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM