char数组的嵌套结构 [英] Nested structure to char array
本文介绍了char数组的嵌套结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨我想在c编程中使用sprintf函数将嵌套结构转换为char数组。
hi i am trying to convert the nested structure to char array using sprintf function in c programming.
struct date_of_birth{
int dd,mm,yy;
};
struct student{
char name[30];
int rollNumber;
date_of_birth dob;
};
int main()
{
// to make a single char aray using sprintf()
char arr[100];
}
我的尝试:
你能告诉我哪个循环将继续学生或dob以形成字符数组?
What I have tried:
can you please let me which loop will continue first either student or dob to form char array?
推荐答案
如果你需要序列化 [ ^ ]struct
然后 memcpy [ ^ ]是要走的路
If you need to serialize[^] thestruct
then memcpy[^] is the way to go
student s;
char a[100];
memcpy(a, &s, sizeof(s));
[更新]修复了错误,感谢 K5054 [/ update]。
[update]Fixed a blunder, thanks to K5054[/update].
一种方式:
One way :
void GetStudentDataString( student * ps )
{
const int bufferSize = 127;
char buffer[bufferSize+1] = {0};
snprintf( buffer, bufferSize, "%s, %d, %02d:%02d:%02d",
ps->name, ps->rollNumber, ps->yy, ps->mm, ps->dd );
}
这篇关于char数组的嵌套结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文