在 C++ 中的 struct 中初始化 char* [英] Initializing a char* in struct in C++
本文介绍了在 C++ 中的 struct 中初始化 char*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个结构如下:
struct temp
{
char* person;
char* address;
int id;
int phone;
}
我有一个结构变量
temp var;
我希望在构造函数中初始化结构的成员,例如:
I wish to initialize the member of the structs in a constructor like:
Data ( )
{
var -> person = {};
var -> address = {};
var.id = 0;
var.phone = 0;
..............
}
person 和 address 是字符数组.
person and address are char arrays.
我不确定这是否是在 C++ 中初始化它们的正确方法.有什么建议吗?
I am not sure if that is the correct way to initialize them in C++. Any suggestions ?
推荐答案
我会简单地将它们设置为 NULL.附带说明一下,我不禁注意到你有 var-> 和 var.混合.如果你创建一个对象,它是 [.] 如果你引用一个指向基对象的指针,它是 [->].
I would simply set them to NULL. On a side note, I couldn't help but notice you had var-> and var. mixed. If you create an object, it is [.] If you are referencing a pointer to the base object it is [->].
typedef struct TTEMP
{
char* person;
char* address;
int id;
int phone;
}TTemp;
Data ( )
{
TTemp var;
var.person = NULL;
var.address = NULL;
var.id = 0;
var.phone = 0;
..............
}
这篇关于在 C++ 中的 struct 中初始化 char*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文