Home > Software design >  How to allocate char poiter to pointer char ** is it possible in C or do I need C for this
How to allocate char poiter to pointer char ** is it possible in C or do I need C for this

Time:07-06

Lets say I have char pointer to pointer now I want to allocate space for 3 pointers. I believe size of C char pointer is also 8 bytes. first pointer sized of 8 bytes will have strings that I will allocate later. I want to allocate memory for 3 pointers so I can access these pointers through a[0][string_num] to a[2][string_num] Then after all that I all allocate what a[0] pointer and a[1] pointer and a[2] pointing what strings

char **a;

I tried something like this. This throws compiler error that

 a = new (char *)[3];

Error

 error: array bound forbidden after parenthesized type-id
   11 |         a = new (char *)[3];
      |                         ^

In C this is possible. is it also possible in C ?

CodePudding user response:

Don't put parentheses around the type.

a = new char *[3];

As an aside, if you are writing C , use std::string for strings, and std::vector for dynamic arrays.

  • Related