0

I have a code that is supposed to get the time , I want to store the time in my char array but I cannot do that so , I thought that I could have a loop to a pointer and loop through the pointer and copy the chars from the pointer memory to my char, is that possible?

void My_Time(char *myt_Time,int size) 
{
    time_t raw_Time = time(0);
    struct tm *info;
    char *myt_Temp;
    int x;

        info = localtime(&raw_Time);
        myt_Temp = asctime(info);
        for (x=0;x<size;x++)
        {
          myt_Time[x]=myt_Temp;
        }
}
2
  • 1
    You need to stop when you reach the trailing NULL in myt_Temp. You can use strncpy to do this, instead of writing your own loop. Commented Apr 4, 2014 at 16:34
  • Also, it should be myt_Time[x] = myt_Temp[x]. Commented Apr 4, 2014 at 16:35

1 Answer 1

2

From what I understand of your problem, the following code should work, instead of your for loop.

strncpy(myt_Time, mytTemp, size);

The bug in your for loop is there:

myt_Time[x]=myt_Temp; => myt_Time[x]=myt_Temp[x];

Sign up to request clarification or add additional context in comments.

1 Comment

SIZE_OF_MYT_TIME should just be size.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.