0

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 ?

noobcoder
  • 11,983
  • 10
  • 39
  • 62

4 Answers4

3

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, i.e. something like this:

delete[] var -> person;
delete[] var -> address;
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Ah, semantic typography - "operator `new`" or "`operator new`"? Don't we love it. – Kerrek SB Jul 14 '14 at 16:33
  • The problem is, I do not know the size of the person array and it is dynamic. – noobcoder Jul 14 '14 at 16:34
  • @noobcoder Then use `std::string`, it does not care about the size - it would take as much or as little as is there, and it will change later if you wish to add or remove some characters. It would also free memory at the end. – Sergey Kalinichenko Jul 14 '14 at 16:36
  • Do i need to use: this -> var.person = NULL or can I avoid the "this" pointer – noobcoder Jul 14 '14 at 16:37
  • @noobcoder Don't use `this` pointer except to disambiguate the expression. In this case, don't use `this->` unless there's also a local variable or a function parameter named `var` that would "shadow" the member variable `var`. – Sergey Kalinichenko Jul 14 '14 at 16:40
2

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).

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Hopelessly offtopic, but I've seriously never seen anyone use spaces around the `->` operator in my life, and now both you and the TS do it. Did I miss a hip new coding standard somewhere? – Niels Keurentjes Jul 14 '14 at 16:31
  • 1
    That's a deprecated conversion which leads to brittle code. – Kerrek SB Jul 14 '14 at 16:32
1

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;
    .............. 
}
Roy Folkker
  • 407
  • 3
  • 8
  • Do i need to use: this -> var.person = NULL or can I avoid the "this" pointer – noobcoder Jul 14 '14 at 16:37
  • This worked. Marking it as the correct answer I was looking for. (y) – noobcoder Jul 14 '14 at 16:39
  • 1
    Inside the member function of that class, `this->` is not required. However, the most IDEs provide the syntax completion, when you begin to type `this->`. That's why you'll often see the redundant `this` in code. – Valentin H Jul 14 '14 at 16:42
  • the -> is used for pointer references. If you want create a TTemp *var = new TTemp;, you would then need to use var->person = NULL, as var would be a pointer. But, since it is an object, you use var.person = NULL; – Roy Folkker Jul 14 '14 at 16:43
  • 1
    -1: What kind of a compiler do you use, which still allows default int as return value (I mean Data() )? And using typedef for structs in C++? What is it good for? (probably for sharing the code between C and C++, when you are hard-ware or a driver programmer). This is IHMO a stone-age C but not C++. – Valentin H Jul 14 '14 at 16:46
  • lol you are correct, I have some antiquated signatures to my code. I do apologize. As for the Data(), I was copy and pasting the OP, and modified it it answer the question, not go for a full rewrite. Again, I apologize for the short-handedness. – Roy Folkker Jul 14 '14 at 16:52
1

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;
}
Valentin H
  • 7,240
  • 12
  • 61
  • 111