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 ?

8
  • 10
    Why are you using old skool C strings if this is supposed to be a C++ program ? Use std::string, as nature intended. Commented Jul 14, 2014 at 16:28
  • It is a pointer, initialize it to nullptr (or NULL if pre-C++11) Commented Jul 14, 2014 at 16:29
  • The current architecture is old school that is why. STD strings would have been easier but I am restricted to use them Commented Jul 14, 2014 at 16:29
  • stackoverflow.com/questions/388242/… Commented Jul 14, 2014 at 16:30
  • 4
    Why are you using -> for some fields and . for others? Commented Jul 14, 2014 at 16:30

4 Answers 4

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;
Sign up to request clarification or add additional context in comments.

5 Comments

Ah, semantic typography - "operator new" or "operator new"? Don't we love it.
The problem is, I do not know the size of the person array and it is dynamic.
@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.
Do i need to use: this -> var.person = NULL or can I avoid the "this" pointer
@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.
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).

2 Comments

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?
That's a deprecated conversion which leads to brittle code.
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;
    .............. 
}

6 Comments

Do i need to use: this -> var.person = NULL or can I avoid the "this" pointer
This worked. Marking it as the correct answer I was looking for. (y)
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.
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;
-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++.
|
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.