1

Concerning a pointer to pointer I wonder if its ok with the following:

    char** _charPp = new char*[10];

     for (int i = 0; i < 10; i++)
    ´   _charPp[i] = new char[100];

That is - if I want a pointer to pointer that points to 10 char-arrays.

The question - is this ok now or do I have to make some kind of initializing of every char-arrays? Then - how do I do that?

I will later on in the program fill these arrays with certain chars but i suspect that the arrays should be initialized with values before such as "0"

6
  • 1
    Your char will each have undefined value. Commented May 12, 2014 at 10:08
  • @Jarod42 - hi, thanks - but how do i initialize those then? Commented May 12, 2014 at 10:09
  • @user3155478, to what value do you want to initialize them? Commented May 12, 2014 at 10:11
  • 2
    Is there a reason you are trying to use pointers when you don't seem to understand them? Why not use a vector? This is C++ after all Commented May 12, 2014 at 10:19
  • 1
    To set to 0 just put () after the ]. Commented May 12, 2014 at 10:48

2 Answers 2

2

Yes, it looks pretty much suitable,

int arraySizeX = 100;
int arraySizeY = 100;
char** array2d = new char*[arraySizeX] // you've just allocated memory that
                                       // holds 100 * 4 (average) bytes
for(int i = 0; i < arraySizeX; ++i)
{
    array2d[i] = new char[arraySizeY] // you've just allocated a chunk that 
                                       //holds 100 char values. It is 100 * 1 (average) bytes
    memset(array2d[i], 0, arraySizeY) // to make every element NULL
}

I will later on in the program fill these arrays with certain chars but i suspect that the arrays should be initialized with values before such as "0"

No, there's no "default value" in C++. You need to assign 0 to the pointer to make it null, or use memset() to do it with arrays.

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

1 Comment

I didn't downvote but it's probably because the memset is more effort than going new char[arraysizeY](); (and could also be slower)
1

Netherwire's answer shows correctly how to initialize the chars to 0. I'll address the first part of your question.

There is no requirement to initialize them yet. As long as you initialize the chars before they're read, it's correct.

Depending on the structure of your code and how you use the arrays, it may be safer to do anyway since it can be hard to find the bug if you eventually do read some of them before initialization.

Also, remember to later delete[] the arrays that you've allocated. Or better yet, consider using std::vector instead (and possibly std::string depending on what you use those arrays for.)

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.