C++ char指针数组 - 无法访问元素

Ton*_*ony 0 c++ arrays pointers

我在我的数组中存储一些char指针时遇到了一些麻烦

该方法应该采用char*数组和int指针,该指针是数组的大小.然后我循环并要求用户输入名称.然后我要打印它们; 但似乎我的阵列中没有任何东西.

注意我不能使用数组表示法进行此赋值,我必须使用指针表示法来访问数组的元素.

void sort_name(char* name[], int* items)
{
  //store the names
    cout << "In function items is " << *items << endl;

    for(int i=0; i<*items; i++)
    {
        string str;
        cout << "Enter name #" << (i+1) << ": ";
        getline(cin, str);

        char* current = new char[str.size() + 1];
        *(name + i) = current;
    }

    for(int i=0; i<*items; i++)
    {
        cout << *(name + i) << endl;
    }



}
Run Code Online (Sandbox Code Playgroud)

K-b*_*llo 5

    char* current = new char[str.size() + 1];
    *(name + i) = current;
Run Code Online (Sandbox Code Playgroud)

您正在为每个数组元素正确分配新内存,但您从不为它们分配任何值.由于这是家庭作业,我不会多说.