如何在阵列中使用双重指针

发布于 2025-01-22 10:57:09 字数 850 浏览 0 评论 0原文

我遇到了一个问题,我必须编写一个可以使字符数组的函数,然后返回数组。...我插图都知道如何使用双重指针...整个代码都在这里:

#include<iostream>
using namespace std;
char** StringTokenize(char*);
int main()
{
    char *string1=new char[50];
    cout<<"Enter: ";
    cin.getline(string1,50);
    StringTokenize(&string1[0]);
    return 0;
}
char** StringTokenize(char *string1)
{
    int i = 0, j = 0;
    char **tokenArray[50];
    **tokenArray[0] = &string1[0];
    while (string1[i] != '\0')
    {
        tokenArray[i] = string1[i];
        i++;
        if (string1[i] == ' ')
        {
            tokenArray[i] = '\n';
            i++;
        }
        else
        {
            continue;
        }
    }
    tokenArray[i] = '\0';
    cout << tokenArray << endl;
    return tokenArray;
}

请帮助我并为我写这个功能...请记住,功能的原型不应更改

I am in a problem in which I have to write a function which will tokenize the array of characters and then return the array.... I cannnot understand how to use double pointers... The whole code is here:

#include<iostream>
using namespace std;
char** StringTokenize(char*);
int main()
{
    char *string1=new char[50];
    cout<<"Enter: ";
    cin.getline(string1,50);
    StringTokenize(&string1[0]);
    return 0;
}
char** StringTokenize(char *string1)
{
    int i = 0, j = 0;
    char **tokenArray[50];
    **tokenArray[0] = &string1[0];
    while (string1[i] != '\0')
    {
        tokenArray[i] = string1[i];
        i++;
        if (string1[i] == ' ')
        {
            tokenArray[i] = '\n';
            i++;
        }
        else
        {
            continue;
        }
    }
    tokenArray[i] = '\0';
    cout << tokenArray << endl;
    return tokenArray;
}

Please help me and write this function for me... Remember that prototype of function should not change

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我ぃ本無心為│何有愛 2025-01-29 10:57:09

然后返回数组。

在C ++中,函数的返回类型不能是数组。

  char *string1 = new char [50];
 

将裸露的指针用于动态记忆是一个坏主意。如果该程序完全编译,您将泄漏内存。我建议改用std :: String

  stringTokenize(&amp; string1 [0]);
 

&amp; string1 [0]是多余的。您可以通过指针间接,然后获取结果的地址……这是您间接的指针。

  int i = 0,j = 0;
 

您已声明J,但没有将其用于任何东西。

  char ** tokenarray [50];
 

子字符串的指针应该是char的指针。目前尚不清楚为什么您的阵列包含指针的指针。

请注意,这些指针目前是不可原始的。

  ** tokenarray [0] =&amp; string1 [0];
 

** Tokenarray [0]将通过第一个非初始化指针间接,并且程序的行为将不确定。右侧具有上述冗余。

类型不匹配。左手操作数的类型为char,右手操作数的类型为char*。这是不良的,毫无意义的。您的编译器应该告诉您这样的错误。

  tokenarray [i] = string1 [i];
 
  tokenarray [i] ='\ n';
 
  tokenarray [i] ='\ 0';
 

类型不匹配。左手操作数的类型是char **,右手操作数为char。这些是不明显的,毫无意义的。您的编译器应该告诉您这样的错误。

 返回tokenarray;
 

这是错误的,因为

  1. 类型不匹配。您应该返回char **,但是tokenarraychar **的数组。
  2. 返回自动阵列将导致隐性转换为指针转换为第一个元素(这是char ***),并且自动阵列将在函数返回后自动销毁,因此返回的指针将无效。

功能的原型不应更改

,因为它可以防止编写设计良好的功能。您要么必须使用静态存储作为数组,要么将裸露的指针返回到动态数组的(第一个元素)。这也不是一个好主意。

and then return the array.

In C++, return type of a function cannot be an array.

char *string1=new char[50];

It's a bad idea to use bare owning pointers to dynamic memory. If the program compiled at all, you would be leaking memory. I recommend using std::string instead.

StringTokenize(&string1[0]);

&string1[0] is redundant. You indirect through a pointer, and then get the address of the result... which is the pointer that you indirected through.

int i = 0, j = 0;

You've declared j, but aren't using it for anything.

char **tokenArray[50];

The pointers to substrings should be pointers to char. It's unclear why your array contains pointers to pointers to char.

Note that these pointers are uninitiallised at the moment.

 **tokenArray[0] = &string1[0];

**tokenArray[0] will indirect through the first uninitialised pointer and the behaviour of the program will be undefined. And the right hand side has the aforementioned redundancy.

And the types don't match. The type of left hand operand is char while the type of right hand operand is char*. This is ill-formed and meaningless. Your compiler should be telling you about bugs like this.

tokenArray[i] = string1[i];
tokenArray[i] = '\n';
tokenArray[i] = '\0';

The types don't match. The type of left hand operand is char** while the type of right hand operand is char. These are ill-formed and meaningless. Your compiler should be telling you about bugs like this.

return tokenArray;

This is wrong because

  1. The types don't match. You're supposed to return a char**, but tokenArray is an array of char**.
  2. Returning an automatic array would result in implicit conversion to pointer to first element (which is a char***) and the automatic array would be automatically destroyed after the function returns, so the returned pointer would be invalid.

prototype of function should not change

That's rather unfortunate since it prevents writing a well designed function. You will either have to use static storage for the array, or return an owning bare pointer to (first element of) dynamic array. Neither is a good idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文