Question

I am new to c and im in a weird situation: I am trying to update a date which I have taken as string input and my code is

typedef struct Employee
{
char fname[20];
char lname[20];
int eme_id;
int emr_id;
char department[20];
int age;
char join_date[20];
float bsal;
float pol_value;
char pol_start_date[20];
char pol_end_date[20];
float premium;
float pre_payment;
char pre_pay_date[20];
int pre_status;/* 0 then not paid 1 then paid*/
float bonus;
}Employee;


char *update_date(char *dat)
{

char *result = NULL;
printf(dat);
result = strtok( dat, "/" );
int date[3];
int i=0;
while( result != NULL ) {
    printf( "result is \"%s\"\n", result );
    date[i] = atoi( result );
    printf( "%d\n", date[i] );
    i++;
    result = strtok( NULL, "/" );
}

if(date[1]!=12)
{
    date[1]++;
}
else
{
    date[1]=1;
    date[2]++;
}

char a[20];
char b[20];
char c[20];
char d[20];
sprintf(a, "%d", date[0]);
sprintf(b, "%d", date[1]);
sprintf(c, "%d", date[2]);

strcpy (d,a);
strcat (d,"/");
strcat (d,b);
strcat (d,"/");
strcat (d,c);

printf(d);
return d;
}

here this function is working just fine but when im calling it in side another function like

while(fread(&eme,recsize_eme,1,fq)==1)
            {

                char *hell;
                hell = update_date(eme.pre_pay_date);
                printf("%s",hell);
            }

now it prints some arbitrary text..:/ plz someone help me out

No correct solution

OTHER TIPS

In the first code

char d[20];
... ... 
return d;

"d[20]" is on the stack. You are returning a pointer to data on the stack. As soon as update_date() returns all of its local variables are now invalid.

In the second you print the value of d[] before returning so there is no problem.

strok() is probably best to avoid. It modifies the string that you pass in and it keeps static state, both of which are serious pitfalls and often cause subtle bugs.

Here are a couple of alternative implementations. (Of course, in production code you should avoid writing code to parse dates/times in the first place. There are OS and library functions to do this. There are many subtleties).

// Scanf can do parsing for you
int date[3];
int n;
n = sscanf(dat, "%d/%d/%d", &date[0], &date[1], &date[2]);
if (n == 3) 
{
    // OK, we got 3 integers...
}


// atoi() stops on non-digits, use it instead of strtok
char *result = dat;
int date[3];
int i = 0;
while (i < 3 && result)
{
    date[i++] = atoi(result);
    result = strchr(result, '/');
    if (result)
    {
        ++result; // Skip the '/'
    }
}

I know this isn't related to your question, but be aware that instead of this:

char a[20];
char b[20];
char c[20];
char d[20];
sprintf(a, "%d", date[0]);
sprintf(b, "%d", date[1]);
sprintf(c, "%d", date[2]);

strcpy (d,a);
strcat (d,"/");
strcat (d,b);
strcat (d,"/");
strcat (d,c);

You can simply do this:

char d[20];
sprintf(d, "%d/%d/%d", date[0], date[1], date[2]);

If you want to be safe, use snprintf:

char d[20];
snprintf(d, sizeof d, "%d/%d/%d", date[0], date[1], date[2]);

An in doing so, you get rid of temporary buffers and eliminate the inefficiency of using strcat. Be aware that whenever you use strcat, the function has to start from the beginning of the string, find the end, and then copy the source string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top