c - 在c中for循环一个指针char

标签 c pointers

我有一个应该获取时间的代码,我想将时间存储在我的 char 数组中,但我不能这样做,我想我可以有一个指向指针的循环并遍历指针并复制从指针内存到我的字符的字符,这可能吗?

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;
        }
}

最佳答案

根据我对您的问题的理解,以下代码应该可以工作,而不是您的 for 循环。

strncpy(myt_Time, mytTemp, 大小);

for 循环中的错误就在那里:

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

关于c - 在c中for循环一个指针char,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22868217/

相关文章:

c - 结构中的数组是否会出现别名/对齐问题?

c++ - 将 QImage 传递给方法 - 指针混淆

c++ - 错误 : request for the member 'query' in connection which is non-class type ' MYSQL*'

c - 错误 : request for member 'valore' in something not a structure or union

c - 为什么要在链表中声明指针?

c - dcgettext 的目的是什么?

c - 以 find 作为参数的 execlp

c - 未赋值的变量有值

c - 二叉树 - 插入和镜像之间的区别?

c - C 上的 SMTP 服务器