Thread: problem with time()

  1. #1
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54

    problem with time()

    Please help me understand the following:

    In the following code time() funtions takes an address value, like time(&instance_of_time_t), and gives present time as output. I am wondering what is ((time_t *)0) in the_time+time((time_t *)0).

    Code:
    time_t the_time;
    for(i = 1; i <= 10; i++) {
    the_time = time((time_t *)0);
    printf("The time is &#37;ld\n", the_time);
    Last edited by jas_atwal; 12-10-2007 at 10:48 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    These two lines do the same thing
    Code:
    time((time_t *)0);
    time(NULL);
    The function time() returns the time value, as well as store it in the place pointed to.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54
    Quote Originally Posted by matsp View Post
    These two lines do the same thing
    Code:
    time((time_t *)0);
    time(NULL);
    The function time() returns the time value, as well as store it in the place pointed to.

    --
    Mats
    Thanks for your reply Mats. Can you please explain me what is time_t * in the fuction call time((time_t *) 0) ?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It's called a cast - which is used in C to convert different types, e.g. (double)100 is the same as 100.0, NULL is the same as (void *)0.

    In C (as opposed to C++) the number 0 isn't definitely sure to be compatible with a pointer type, so it's got a cast to tell the compiler "Look, it's the number zero, but it REALLY is a time_t pointer with value zero, not an integer with value zero". It's just a homemade (and more specific) version of NULL.



    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54

    Thumbs up

    Quote Originally Posted by matsp View Post
    It's called a cast - which is used in C to convert different types, e.g. (double)100 is the same as 100.0, NULL is the same as (void *)0.

    In C (as opposed to C++) the number 0 isn't definitely sure to be compatible with a pointer type, so it's got a cast to tell the compiler "Look, it's the number zero, but it REALLY is a time_t pointer with value zero, not an integer with value zero". It's just a homemade (and more specific) version of NULL.



    --
    Mats
    This answers my query, Thank you so much!

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A cast is usually made to tell the compiler that you want this conversion to be done and that you know what you're doing (which is why a cast can also be dangerous if misused).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54
    Thanks Elysia

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer problem... i think
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 11-30-2005, 03:45 PM
  2. another problem with catstr() this time
    By the_winky_files in forum C Programming
    Replies: 19
    Last Post: 09-22-2005, 04:20 PM
  3. time problem
    By sand_man in forum C Programming
    Replies: 9
    Last Post: 09-13-2005, 05:59 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. Problem with time on the board, or is it just me?
    By biosninja in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 10-23-2002, 05:45 AM