简体   繁体   中英

Initializing a char* in struct in C++

I have a struct as follows:

struct temp 
{
    char* person;
    char* address;
    int id;
    int phone;
}

And I have a struct variable as

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 and address are char arrays.

I am not sure if that is the correct way to initialize them in C++. Any suggestions ?

A proper approach in C++ is to use std::string in place of char* .

struct temp 
{
    std::string person;
    std::string address;
    int id;
    int phone;
}

This change would ensure that the resources for your string are managed automatically.

If you must use C strings as part of a learning exercise, and you wish to initialize them to an empty string (as opposed to a NULL pointer), you can use operator new[] , like this:

Data ( )
{
    var -> person = new char[1];
    var -> person[0] = 0;
    var -> address = new char[1];
    var -> address[0] = 0;
    var.id = 0;
    var.phone = 0;
}

If you take this approach, you need to add code that de-allocates the memory once you are done with your var , ie something like this:

delete[] var -> person;
delete[] var -> address;

If you want to set the pointer field to NULL (or nullptr in C++11) try

 var -> person = NULL;

If you want to set it to a literal constant string, try something like

 var -> person = "John Doe";

(As Paul R commented, make that a std::string )

Better yet, make your struct temp an authentic class so provide constructors and destructors to it. Read about the Rule of three (in C++11, it is becoming the rule of five).

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

Struct in C++ is (almost) the same as class, but with all members public by default. You can also initialize in a constructor.

struct temp 
{
    char* person;
    char* address;
    int id;
    int phone;
    temp():person(0),address(0),id(0),phone(0){}
}

temp t; // initialized!

If for some reasons you don't want to have a constructor, create a factory function, which will encapsulate the initialization of the members and create the instances only there. This used to be actually the way in C to mimic the constructors.

temp* create_my_temp()
{
  temp *t;
  //create using malloc or new
  //initialize t;
  return t;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM