time function not defined?

G

Greg

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

Régis Troadec

"Greg" <[email protected]> a écrit dans le message de

Hi,

[snipped]
int main (void) {
char *time ;

time_t time_now = time (NULL) ;

Take a closer look at the two lines above and you'll get the answer.
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?

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
 
H

Huilong Huang

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
 
P

P.J. Plauger

Greg said:
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 ;
^^^^ local redefinition of time
time_t time_now = time (NULL) ;
^^^^ library function is masked
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?

The compiler is telling you. See comments above.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
M

Martin Ambuhl

Greg said:
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 ;
^^^^
You have changed 'time' from referring to a function to referring to a
pointer-to-char.
time_t time_now = time (NULL) ;
^^^^
The pointer-to-char identifier 'time' is not a function name in this scope.
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

Obviously. pointers-to-char are not functions.
 
K

Keith Thompson

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?

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.
 
D

Dan Pop

In said:
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?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top