Search code examples
c++arraysc++11crtc-strings

How to initialize c-strings array (no stl)


I want to initialize array of c-strings with zero pointers in MSVC2010

// Foo.h
#pragma once
class Foo {
  int sz_;
  char **arr_; 
public:
  Foo();
  ~Foo();
  // ... some other functions
};

// Foo.cpp
#include "Foo.h"
#define INITIAL_SZ 20

Foo::Foo() : sz_(INITIAL_SZ) {
  // there I have to initialize arr_ (dynamic array and can be enlarged later)
  arr_ = (char **)calloc(INITIAL_SZ * sizeof (char *)); // ??? 
  // or maybe arr_ = new ...
}

How to correct initialize arr_? I was not allowed to use of STL, MFC, etc.


Solution

  • arr = new char*[INITIAL_SZ](); will do - you can even put it in an initialization list.