time function not defined?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Greg

    #1

    time function not defined?

    I am using the following code to try and output the current date and
    time in a C program.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>

    // global variables

    struct tm *currentTime ;

    int main (void) {
    char *time ;

    time_t time_now = time (NULL) ;

    currentTime = localtime (&time_now) ;
    }

    When I compile this I get the following:

    time.c: In function `main':
    time.c:12: called object is not a function

    Anyone know why the time function is not working for me?

    Thanks

    Greg
  • Régis Troadec

    #2
    Re: time function not defined?


    "Greg" <djbitchpimp@sn owboard.com> a écrit dans le message de
    news:7e312ede.0 404211329.37a81 204@posting.goo gle.com...

    Hi,

    [snipped]
    [color=blue]
    > int main (void) {
    > char *time ;
    >
    > time_t time_now = time (NULL) ;[/color]

    Take a closer look at the two lines above and you'll get the answer.
    [color=blue]
    > currentTime = localtime (&time_now) ;
    > }
    >
    > When I compile this I get the following:
    >
    > time.c: In function `main':
    > time.c:12: called object is not a function
    >
    > Anyone know why the time function is not working for me?[/color]

    Yes, you declared a pointer to a char named time, which *is in conflict with
    the name* of the time() function of the standard library.

    Regis
    [color=blue]
    > Thanks
    >
    > Greg[/color]


    Comment

    • P.J. Plauger

      #3
      Re: time function not defined?

      "Greg" <djbitchpimp@sn owboard.com> wrote in message
      news:7e312ede.0 404211329.37a81 204@posting.goo gle.com...[color=blue]
      > I am using the following code to try and output the current date and
      > time in a C program.
      >
      > #include <stdio.h>
      > #include <stdlib.h>
      > #include <string.h>
      > #include <time.h>
      >
      > // global variables
      >
      > struct tm *currentTime ;
      >
      > int main (void) {
      > char *time ;[/color]
      ^^^^ local redefinition of time[color=blue]
      >
      > time_t time_now = time (NULL) ;[/color]
      ^^^^ library function is masked[color=blue]
      >
      > currentTime = localtime (&time_now) ;
      > }
      >
      > When I compile this I get the following:
      >
      > time.c: In function `main':
      > time.c:12: called object is not a function
      >
      > Anyone know why the time function is not working for me?[/color]

      The compiler is telling you. See comments above.

      P.J. Plauger
      Dinkumware, Ltd.



      Comment

      • Huilong Huang

        #4
        Re: time function not defined?

        Hi, Greg,

        You defined a variable " char *time ", which has the same name of the
        function time(), hence when you refer the name "time", it actually refers to
        the variable "char *time" in that scope.

        Huilong


        "Greg" <djbitchpimp@sn owboard.com> wrote in message
        news:7e312ede.0 404211329.37a81 204@posting.goo gle.com...[color=blue]
        > I am using the following code to try and output the current date and
        > time in a C program.
        >
        > #include <stdio.h>
        > #include <stdlib.h>
        > #include <string.h>
        > #include <time.h>
        >
        > // global variables
        >
        > struct tm *currentTime ;
        >
        > int main (void) {
        > char *time ;
        >
        > time_t time_now = time (NULL) ;
        >
        > currentTime = localtime (&time_now) ;
        > }
        >
        > When I compile this I get the following:
        >
        > time.c: In function `main':
        > time.c:12: called object is not a function
        >
        > Anyone know why the time function is not working for me?
        >
        > Thanks
        >
        > Greg[/color]


        Comment

        • Martin Ambuhl

          #5
          Re: time function not defined?

          Greg wrote:
          [color=blue]
          > I am using the following code to try and output the current date and
          > time in a C program.
          >
          > #include <stdio.h>
          > #include <stdlib.h>
          > #include <string.h>
          > #include <time.h>
          >
          > // global variables
          >
          > struct tm *currentTime ;
          >
          > int main (void) {
          > char *time ;[/color]
          ^^^^
          You have changed 'time' from referring to a function to referring to a
          pointer-to-char.
          [color=blue]
          > time_t time_now = time (NULL) ;[/color]
          ^^^^
          The pointer-to-char identifier 'time' is not a function name in this scope.
          [color=blue]
          >
          > currentTime = localtime (&time_now) ;
          > }
          >
          > When I compile this I get the following:
          >
          > time.c: In function `main':
          > time.c:12: called object is not a function[/color]

          Obviously. pointers-to-char are not functions.

          Comment

          • Keith Thompson

            #6
            Re: time function not defined?

            djbitchpimp@sno wboard.com (Greg) writes:[color=blue]
            > I am using the following code to try and output the current date and
            > time in a C program.
            >
            > #include <stdio.h>
            > #include <stdlib.h>
            > #include <string.h>
            > #include <time.h>
            >
            > // global variables
            >
            > struct tm *currentTime ;
            >
            > int main (void) {
            > char *time ;
            >
            > time_t time_now = time (NULL) ;
            >
            > currentTime = localtime (&time_now) ;
            > }
            >
            > When I compile this I get the following:
            >
            > time.c: In function `main':
            > time.c:12: called object is not a function
            >
            > Anyone know why the time function is not working for me?[/color]

            Several other people have told you that the problem is your
            redefinition of "time" as a char* variable. But for future reference,
            if you post a code sample and an error message that indicates a line
            number, let us know where line 12 really is. The line containing the
            call to time() is actually the 13th line of the code you posted.

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
            Schroedinger does Shakespeare: "To be *and* not to be"

            Comment

            • Dan Pop

              #7
              Re: time function not defined?

              In <7e312ede.04042 11329.37a81204@ posting.google. com> djbitchpimp@sno wboard.com (Greg) writes:
              [color=blue]
              >I am using the following code to try and output the current date and
              >time in a C program.
              >
              >#include <stdio.h>
              >#include <stdlib.h>
              >#include <string.h>
              >#include <time.h>
              >
              >// global variables
              >
              >struct tm *currentTime ;
              >
              >int main (void) {
              > char *time ;
              >
              > time_t time_now = time (NULL) ;
              >
              > currentTime = localtime (&time_now) ;
              >}
              >
              >When I compile this I get the following:
              >
              >time.c: In function `main':
              >time.c:12: called object is not a function
              >
              >Anyone know why the time function is not working for me?[/color]

              In C, object names share the same name space with function names.
              When you declared time as a char pointer in main, you have suppressed the
              declaration of time coming from <time.h> until the end of the block
              containing the new declaration (i.e. until the end of the function main
              in your case).

              IOW, you have shot yourself in the foot.

              Dan
              --
              Dan Pop
              DESY Zeuthen, RZ group
              Email: Dan.Pop@ifh.de

              Comment

              Working...