Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to allocate char poiter to pointer char ** is it possible in C++ or do I need C for this

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading