如何在阵列中使用双重指针
我遇到了一个问题,我必须编写一个可以使字符数组的函数,然后返回数组。...我插图都知道如何使用双重指针...整个代码都在这里:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在C ++中,函数的返回类型不能是数组。
将裸露的指针用于动态记忆是一个坏主意。如果该程序完全编译,您将泄漏内存。我建议改用
std :: String
。&amp; string1 [0]
是多余的。您可以通过指针间接,然后获取结果的地址……这是您间接的指针。您已声明
J
,但没有将其用于任何东西。子字符串的指针应该是char的指针。目前尚不清楚为什么您的阵列包含指针的指针。
请注意,这些指针目前是不可原始的。
** Tokenarray [0]
将通过第一个非初始化指针间接,并且程序的行为将不确定。右侧具有上述冗余。类型不匹配。左手操作数的类型为
char
,右手操作数的类型为char*
。这是不良的,毫无意义的。您的编译器应该告诉您这样的错误。类型不匹配。左手操作数的类型是
char **
,右手操作数为char
。这些是不明显的,毫无意义的。您的编译器应该告诉您这样的错误。这是错误的,因为
char **
,但是tokenarray
是char **
的数组。char ***
),并且自动阵列将在函数返回后自动销毁,因此返回的指针将无效。,因为它可以防止编写设计良好的功能。您要么必须使用静态存储作为数组,要么将裸露的指针返回到动态数组的(第一个元素)。这也不是一个好主意。
In C++, return type of a function cannot be an array.
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.&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.You've declared
j
, but aren't using it for anything.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]
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 ischar*
. This is ill-formed and meaningless. Your compiler should be telling you about bugs like this.The types don't match. The type of left hand operand is
char**
while the type of right hand operand ischar
. These are ill-formed and meaningless. Your compiler should be telling you about bugs like this.This is wrong because
char**
, buttokenArray
is an array ofchar**
.char***
) and the automatic array would be automatically destroyed after the function returns, so the returned pointer would be invalid.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.