hi all, im having some problem , returning the buffer array to the main function, im new at this , i need to print the currentdate from the main function, is thr any way i can return the date to the main function ??

#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char getcurrentdate();
char currentdate[30] = getcurrentdate();
printf("date is%s",currentdate);
}
char getcurrentdate()
{
  char buffer[30];
  struct timeval tv;
  time_t curtime;
  gettimeofday(&tv, NULL); 
  curtime=tv.tv_sec;
  strftime(buffer,30,"%d-%m-%y",localtime(&curtime));
  printf("%s\n",buffer);
  return (*buffer);
}

Instead of declaring the buffer locally, pass it to your getcurrentdate function. That way you don't have to worry about returning it:

#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>

char getcurrentdate(char buffer[]);

int main(void)
{
    char currentdate[30];
    getcurrentdate(currentdate);
    printf("date is%s",currentdate);
}
char getcurrentdate(char buffer[])
{
    struct timeval tv;
    time_t curtime;
    gettimeofday(&tv, NULL); 
    curtime=tv.tv_sec;
    strftime(buffer,30,"%d-%m-%y",localtime(&curtime));
    printf("%s\n",buffer);
    return (*buffer);
}

Your function returns the 1st character only. Didn't you know that char type is not the same as a pointer to char (array) char* type?

Pass a char array (date string buffer) and its size as function arguments, for example:

void getcurrentdate(char* date, int datesize)
{
    ... check up buffer size then fill date ...
}
...
int main(void)
{
    char date[30];
    getcurrentdate(date,sizeof date);
    ...
}

It's possible to allocate array dynamically then return a pointer but it's a bad practice to allocate then free memory in different contexts...

>Naure

I used devc++ with the proposed changes but it has some problem with the recognition of timeval struct itself,and also wit the usage of tv.tv_sec.Any suggestions with regard to usage and also the good compiler to use???

>but it has some problem with the recognition of timeval
>struct itself,and also wit the usage of tv.tv_sec.
You shouldn't expect non-standard libraries to work everywhere. You need a compiler/library/system that supports POSIX.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.