1:我想把C++的string[]转换为char *array[],但是试了很久发现无法完成该功能,下面是我调试的代码,无论怎么调试,输出的数组总是不断的循环,不清楚哪里问题,请大家帮忙,谢谢!
#include <iostream>
#include <string.h>
using namespace std;
int main() {
char *instrumentID[] = { 0 }; //订阅合约所以数量为3
string array[] = { "1yuanpengfei", "2yuwangying", "3huangtilong", "4hongzhaoyang", "5gengchaogang", "6zhangshuai", "7shenshuai" };
cout << "total string size is:" << sizeof(array) / sizeof(string) << endl;
int size = sizeof(array) / sizeof(string);
int i;
for (i = 0; i < size; i++) {
const char *charResult = array[i].c_str();
instrumentID[i] = new char[strlen(charResult) + 1];
strcpy(instrumentID[i], charResult);
cout << instrumentID[i] << endl;
}
}
Array out of bounds
The size of this array is only 1.
can be written as
The asterisk in the comment was eaten....