6 Replies - 1642 Views - Last Post: 15 January 2015 - 07:29 AM Rate Topic: -----

#1 daplan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 07-January 15

Current date and time

Posted 14 January 2015 - 08:40 AM

Hello Everyone

I am trying to use to save the current date and time into a char array using this code:

char current_time(char *date){

    time_t result = time(NULL);

    date = ctime(&result);

    return *date;
}
main{

char date [24];

current_time(date);

return 0;



now when I output the date char array into a txt file I only get gibberish. can anyone point me in the right direction?

Is This A Good Question/Topic? 0
  • +

Replies To: Current date and time

#2 jimblumberg  Icon User is offline

  • member icon

Reputation: 5466
  • View blog
  • Posts: 17,017
  • Joined: 25-December 09

Re: Current date and time

Posted 14 January 2015 - 09:15 AM

There are several things wrong with your snippet. The biggest problem you have is that you are passing a statically allocated array into your function and trying to modify the address of this array. Another thing wrong it that you told the compiler you would be returning a single character not a pointer. You may want to try something like:

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


char* current_time(char **date)
{
    time_t result = time(NULL);

    *date = ctime(&result);

    return *date;
}

int main()
{
   char *date;
   printf("%s", current_time(&date));

   return 0;
}



And remember if you call ctime() again it will alter the value pointed to by your pointer.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 daplan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 07-January 15

Re: Current date and time

Posted 14 January 2015 - 01:53 PM

Hi Jim

Thank you very much for your answer. I am not sure if I quite understand what your are saying. I try to pass the address of the char array to the function which is putting the address into a pointer. At least this is my understanding. This is cross referencing, no? I couldn't really get your code to work and I was able to create something like you did using printf. Tough my idea would be to store the time/date inside a char array so it can be reused. I think it could be that I am making a mistake by just trying to put the result of ctime() into the date array. Does this function actually return a set of characters? Maybe that is something that doesnt work in C.

Guess I need to wrap my head around this for a bit longer :).
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon

Reputation: 5466
  • View blog
  • Posts: 17,017
  • Joined: 25-December 09

Re: Current date and time

Posted 14 January 2015 - 02:25 PM

Quote

I couldn't really get your code to work and I was able to create something like you did using printf.

Why couldn't you get the code I provided to work? Show what you tried, the code I provided compiles and works properly.

Quote

I try to pass the address of the char array to the function

You were passing a copy of the address of the character array to the function, so any changes made to this copy will not be reflected in the calling function. If you actually try to change the actual pointer to the statically allocated array you would have at minimum a memory leak.

Quote

Tough my idea would be to store the time/date inside a char array so it can be reused.

Then what did you do to accomplish this?

Quote

I think it could be that I am making a mistake by just trying to put the result of ctime() into the date array.

Yes that was a mistake. The ctime() function returns a pointer to an internally statically allocated buffer.

To store the value returned from ctime() to an array you need to use strcpy().

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


char* current_time(char **date)  // Note the double pointer!
{

    time_t result = time(NULL);

    *date = ctime(&result);

    return *date;
}

int main()
{

   char *date_ptr;
   char date[25];
   current_time(&date_ptr);
   strcpy(date, date_ptr);

   // Or.
   strcpy(date, current_time(&date_ptr));

   printf("%s", date);

   return 0;
}



Jim

This post has been edited by jimblumberg: 14 January 2015 - 02:26 PM

Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Current date and time

Posted 14 January 2015 - 02:53 PM

You could copy the data from timer's buffer inside your function if you want.

char* current_time(char *date) 
{
    time_t result = time(NULL);

    strcpy( date, ctime(&result));

    return *date;
}




The buffer will need to be of size 26 at least to include a newline and null caharacter.
Was This Post Helpful? 0
  • +
  • -

#6 daplan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 07-January 15

Re: Current date and time

Posted 15 January 2015 - 01:26 AM

Thank you very much Jim for your persistence :).

I could get it to run because I was confused by the double pointers in the function headers.

I might get pointers mixed up to much but is if I send a copy of a address to a function wouldn't it still be the same address in the background? How is the memory leak to understand?

Your final code did exactly what I wanted to do. Just for my understanding am I right that the code is doing the following:

- defining of char pointer and char array
- calling of function current_time and passing the address of the pointer date_ptr
- the current_time function is a char pointer function
- now the address of the date_ptr is put into a new pointer??? (I am having troubles understanding the meaning of the double pointer **)
- now time(NULL) is writen into the the result variable
- the function ctime uses the content inside the address of the result variable. the result will be stored under a certain adress that is passed to the date pointer.
- then the date pointer is returned so where ever the date pointer points to, the date_ptr pointer will point as well right?

@ #define

to do your code I need to pass the date char array to the function right? but how can that work, since it is a pointer function...
thanks for the tip on the size of the array, miscounted there.

sorry for asking so many question but you guys really bring me ahead.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon

Reputation: 5466
  • View blog
  • Posts: 17,017
  • Joined: 25-December 09

Re: Current date and time

Posted 15 January 2015 - 07:29 AM

Quote

I might get pointers mixed up to much but is if I send a copy of a address to a function wouldn't it still be the same address in the background?

No when you pass the pointer by value the pointer is copied so you're working with a different address.

Quote

- defining of char pointer and char array
- calling of function current_time and passing the address of the pointer date_ptr

Yes.

Quote

- the current_time function is a char pointer function

The current_time function is returning a char pointer, and you are passing the address to the date_ptr pointer to the function.

Quote

now time(NULL) is writen into the the result variable

Yes the current time is written to the result variable.

Quote

- the function ctime uses the content inside the address of the result variable. the result will be stored under a certain adress that is passed to the date pointer.

Interprets the value pointed by result as a calendar time and converts it to a C-string containing a human-readable version of the corresponding time and date, in terms of local time. The value returned from ctime points to an internal array whose validity or value may be altered by any subsequent call to asctime or ctime.

Quote

then the date pointer is returned so where ever the date pointer points to, the date_ptr pointer will point as well right?

Because I returned the same pointer as the parameter (date) both the returned pointer and the parameter variable will point to the same location. But this function could have been defined to return a void and you could use the parameter would still hold the address of the current time.


Quote

@ #define

to do your code I need to pass the date char array to the function right? but how can that work, since it is a pointer function...
thanks for the tip on the size of the array, miscounted there.


This works because you are altering the string not the pointer. Remember in C arrays are always passed by pointer. The following are all the same:

void date(char *value);
void date(char value[]);
void date(char value[25]);
void date(char *SomeOtherName);




Jim

This post has been edited by jimblumberg: 15 January 2015 - 07:49 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1