C++如何将string数组拷贝到char *array[]字符串数组呢?
黄舟
黄舟 2017-04-17 13:57:27
0
1
655

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;
    }
}

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(1)
伊谢尔伦

Array out of bounds

char *instrumentID[] = { 0 };

The size of this array is only 1.

can be written as

char **instrumentID = new char *[size];

The asterisk in the comment was eaten....

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template