1

I want to use new operator to create an array of strings. But I am not able to figure out how to do this using a single command.

I can do this using calloc like arg = (char*) calloc(totalarg_velanalyze, 5);

But I have been told to use new instead.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
Pratik Kumar
  • 67
  • 1
  • 7
  • Add the language tag, is it C++? – Yu Hao May 22 '14 at 04:45
  • If your school teaches you arrays and `new` before `std::vector` and `std::string`, consider running away while screaming loudly. – n. m. could be an AI May 22 '14 at 05:02
  • Is the purpose of this exercise to allocate N number of M-length fixed char buffers? When you say "string" to a room full of C++ engineers, `std::string` leaps to the forefront. When you say "array", either `std::array<>` or `std::vector<>` likewise leap to the head of the line. Clarify the intention and your attempt at using `operator new` (failure is an option) and we'll see what we can do for you. – WhozCraig May 22 '14 at 05:02
  • Thanks for responding. My purpose is to create a 2d character array to store strings like char a[10][]={"abc","def".......}; But I want to do this dynamic as I dont know the size 10 in this case. – Pratik Kumar May 22 '14 at 05:13

2 Answers2

2

If the intention is to allocate a contiguous array of fixed-length char arrays (what you call a 2D char array), the syntax isn't terrible. The row width must be a compile-time constant, but the number of rows can be arbitrary. Given some arbitrary primary dimension n and some fixed size row-width M, you would do it like this:

char (*p)[M] = new char[n][M];

and delete it like this when no longer needed:

delete [] p;

Access to any buffer i from 0..n-1 is done as:

p[i]

such as copying "foo" to the third row:

std::strcpy(p[2], "foo");

just as you would with a regular fixed array of arrays.

That said, I would advise that unless you have a compelling reason to do otherwise (some specific odd legacy API or some such) that you use the standard library containers. They really are the cats whiskers. At a minimum you should use smart pointers, as raw pointers should not own resources.

Hope it helps (particularly heeding the last paragraph).

Community
  • 1
  • 1
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
0

If you are using C++ its best to use vector of strings.

You can declare it as

vector<string> varname(length);
varname[0]="xyz";
varname[1]="dfg";

or you can let the length be unspecified and add elements using push_back();

coder hacker
  • 4,819
  • 1
  • 25
  • 50
  • To clarify, if you don't supply `length` to the constructor, then the length starts off as `0`; and in both cases you can extend it by doing `push_back` – M.M May 22 '14 at 04:50
  • I would like to use various functions like strmcmp etc. char a[]="abc"; strcmp(a,varname[0]); How is that possible using vectors. – Pratik Kumar May 22 '14 at 05:38
  • @user3649296 You are supposed to write `a == varname[0]`, but you could also write `strcmp(a, varname[0].c_str())`. A C++ `string` is not a `char *`, but can give you a `char *` with `.c_str()`. They seem to be teaching you C instead of C++. Those languages have different ways of doing things and you are doing it the C way in a C++ program which is considered bad. – nwp May 22 '14 at 05:52