如何在头文件中的结构体中为数组初始化和分配内存?
我的问题是这样的,在 test.h
Cpp 文件中有一个 struct data
struct data {
static int year[10];
static int month[12];
static int day[31];
};
,我有几个需要调用它的函数。我应该在哪里以及如何初始化它?
void test::display(){
struct data pointer;
/* ... */
// These three arrays should be initialized
pointer.year[index1] = Timeyear;
pointer.month[index2] = Timemonth;
pointer.day[index3] = Timeday;
printf("%d %d %d", pointer.year[index1],
pointer.month[index2], pointer.day[index3]);
/* ... */
}
My question is like this, there is a struct data
in test.h
struct data {
static int year[10];
static int month[12];
static int day[31];
};
In Cpp file, I have several functions which need to call it. Where and how should I initialize it?
void test::display(){
struct data pointer;
/* ... */
// These three arrays should be initialized
pointer.year[index1] = Timeyear;
pointer.month[index2] = Timemonth;
pointer.day[index3] = Timeday;
printf("%d %d %d", pointer.year[index1],
pointer.month[index2], pointer.day[index3]);
/* ... */
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎对 C 和 C++ 感到非常困惑。
printf
?这很糟糕。真的很糟糕。结构数据指针
?放弃结构
。另外,如果您想初始化它,只需使用构造函数。
You appear to be very confused between C and C++.
printf
? It's bad. Really bad.struct data pointer
? Ditch thestruct
.Also, if you want to initialize it, just use a constructor.
我想你的问题已经被STL解决了。
您应该了解有关 std::time_t 对象的更多信息
另请参阅:
time_t 的字符串表示形式?
如何获取当地的使用 boost::date_time 从 time_t 获取日期时间?
time_t 转换格式问题
I think your problem has already been taken care of by the STL.
You should learn more about std::time_t objects
See also:
String representation of time_t?
How to get a local date-time from a time_t with boost::date_time?
time_t conversion format question