buf赋值结构体问题

C语言用字符串直接给结构体赋值方法

struct sTime

{
    char year[4];

    char month[2];

    char day[2];

};
int main()
{
    char buf[8]="20130525";

    struct sTime stime;

    memcpy(&stime,buf,8);

    printf("year: %s\n",stime.year);
    printf("month: %s\n",stime.month);
    printf("day: %s\n",stime.day);
}

结果:
    year:20130525

    month:0525

    day:25

反复尝试,都不行,包括加'\0',是否结构体定义的时候,每个成员变量的大小都加1,如果先把已经赋值好的结构体传给buf,然后用buf再赋值给结构体,就正常,请大虾们给的正确方法。

  https://wenwen.sogou.com/z/q705387572.htm

猜你喜欢

转载自www.cnblogs.com/hshy/p/12566461.html
今日推荐