0

I want to create NULL terminated array in the constructor.

class Test
{
    char name [30];
    char Address[100]
};

Test::Test()
{
    memset(name ,0, 30);
    memset(Address, 0, 100);
}

Is this the correct way to initialize an array to NULL?

Is there any good alternative to this?

6
  • are you trying to replicate c-strings? Commented Dec 19, 2011 at 19:42
  • 2
    This hardly deserves to be called C++. Commented Dec 19, 2011 at 19:45
  • common Guys c string can be used inside C++ classes Commented Dec 19, 2011 at 19:46
  • 3
    You asked for a "good alternate" (sic). If you don't want to accept good advice, why do you ask for it? The code you've written is terrible, and you aren't doing anyone a favour by trying to salvage it. That's not how one writes C++, period. Commented Dec 19, 2011 at 19:47
  • please use consistent data member naming, ie "address". Commented Dec 19, 2011 at 19:48

5 Answers 5

5

If you're planning on using C-style strings, you need only set the first character to a null terminator.

name[0] = Address[0] = 0;

But, in the long run, you will be better off using std::string instead.

Sign up to request clarification or add additional context in comments.

Comments

5

I'd probably do this:

class Test
{
    std::string name;
    std::string address;
};

6 Comments

I can't use STL.Suggest something without STL
@Chris_vr: How not? Imagine it's not STL, it's the C++ standard library.
@Chris_vr: -1 for failing to specify your arcane constraints in your question and then complaining about a perfect answer.
@Chris_vr: Why are you allowed to use some parts of the C++ standard library (memset) and not others (std::string)? Are you using C++ or not?
@NicolBolas I am not aware memset is function of C++.Anyway I need to change the code
|
4

The proper C++ idiom is to value-initialize your C-strings in your constructor's initialization list:

class Test
{
    char name[30];
    char address[100];

public:
    Test();
};

Test::Test()
  : name(),
    address()
{ }

This will have the net effect of all elements of Test::name and Test::address being set to '\0'.

Of course, it would be even better to avoid raw C-strings in the first place, but other answers have already made that point...

1 Comment

Thanks I guess this is better approch.
1

To store strings, it's sufficient put first char to 0. I.e.

Test::Test()
 {
      name[0] = Address[0] = 0;
 }

If you want (for some specific your purpose) to fill the entire arrays, use sizeof to avoid hardcoding indexes.

Test::Test()
 {
      memset(name, 0, sizeof(name));
      memset(Address, 0, sizeof(Address));
 }

Comments

0

I don't know exactly what you want to do, but I should do:

Test::Test()
{
   name[0] = 0;
   Address[0] = 0;
}

in this way you can interpret your variable as empty string.

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.