I declare a array of pointers:
char *a[13];
And how can I allocate memory for it using "new"?
(I'm a Chinese student,so my expression may be not so exact~)
Your question is vague, and there are multiple answers. Can you be more specific as to how you intend to use a?
Compiler doesn't allow to convert pointer ( operator new always returns pointer ) to array type ( char* [13] ), so only way is to declare pointer:
char** a=new char*[13];
a[0]="qwerty";
delete[] a;
Compiler doesn't allow to convert pointer ( operator new always returns pointer ) to array type ( char* [13] ), so only way is to declare pointer:
char** a=new char*[13];
a[0]="qwerty";
delete[] a;
So I use **a too.
Thanks:)
A much safer way to get an array of strings is with the standard vector and string classes:
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::vector<std::string> a;
a.push_back("qwerty");
std::cout<< a[0] <<std::endl;
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.