C++ 返回C+中的char*数组+;
要从函数返回char*数组,我有以下定义。它不是编译的,所以我需要专家的建议来修改它。我的最终目标是从函数中获得char*数组C++ 返回C+中的char*数组+;,c++,C++,要从函数返回char*数组,我有以下定义。它不是编译的,所以我需要专家的建议来修改它。我的最终目标是从函数中获得char*数组 char * []GetItems() { const char *Items[] = {"Item1" , "Item2" , "Item3"}; //processing items.. return Items } 谢谢您的帮助。这里有三个问题: 要在c/c++中返回数组,只需使用另一个*,而不是[] 返回类型需要标记为const,因为您返回的是cons
char * []GetItems()
{
const char *Items[] = {"Item1" , "Item2" , "Item3"};
//processing items..
return Items
}
谢谢您的帮助。这里有三个问题:
*
,而不是[]
const
,因为您返回的是const
变量// Note the return type is const char**
const char** GetItems()
{
// Note this is still a local variable - you'll have to deal with that
static const char *Items[] = {"Item1" , "Item2" , "Item3"};
//processing items..
return Items;
}
这里有三个问题:
*
,而不是[]
const
,因为您返回的是const
变量// Note the return type is const char**
const char** GetItems()
{
// Note this is still a local variable - you'll have to deal with that
static const char *Items[] = {"Item1" , "Item2" , "Item3"};
//processing items..
return Items;
}
这里有三个问题
char**
。这并不意味着有一个大小,因此您将需要一个哨兵(例如,NULL
)在结尾。或者使用<代码> STD::C++中的向量< /代码> .< /P> const
声明,但在没有强制转换的情况下将其作为非常量返回。如果正在处理,请删除const
#include <stdio.h>
#include <stdlib.h>
// Use char* casts to enable the 'processing' later, i.e. do not use
// const strings
char *Items[] = { (char*) "Item1", (char*) "Item2", (char*) "Item3", NULL };
char **
GetItems ()
{
//processing items..
return Items;
}
int
main (int argc, char **argv)
{
int i;
char **items = GetItems ();
for (i = 0; items[i]; i++)
{
printf ("Item is %s\n", items[i]);
}
exit (0);
}
#包括
#包括
//使用char*cast稍后启用“处理”,即不使用
//常量字符串
char*Items[]={(char*)“Item1”,“char*)“Item2”,“char*)“Item3”,NULL};
煤焦**
GetItems()
{
//处理项目。。
退货项目;
}
int
主(内部argc,字符**argv)
{
int i;
char**items=GetItems();
对于(i=0;项[i];i++)
{
printf(“项目为%s\n”,项目[i]);
}
出口(0);
}
这里有三个问题
const
声明,但在没有强制转换的情况下将其作为非常量返回。如果正在处理,请删除const
#include <stdio.h>
#include <stdlib.h>
// Use char* casts to enable the 'processing' later, i.e. do not use
// const strings
char *Items[] = { (char*) "Item1", (char*) "Item2", (char*) "Item3", NULL };
char **
GetItems ()
{
//processing items..
return Items;
}
int
main (int argc, char **argv)
{
int i;
char **items = GetItems ();
for (i = 0; items[i]; i++)
{
printf ("Item is %s\n", items[i]);
}
exit (0);
}
#包括
#包括
//使用char*cast稍后启用“处理”,即不使用
//常量字符串
char*Items[]={(char*)“Item1”,“char*)“Item2”,“char*)“Item3”,NULL};
煤焦**
GetItems()
{
//处理项目。。
退货项目;
}
int
主(内部argc,字符**argv)
{
int i;
char**items=GetItems();
对于(i=0;项[i];i++)
{
printf(“项目为%s\n”,项目[i]);
}
出口(0);
}
<代码> > p>不能在C++中使用数组作为返回参数
您应该使用char **作为返回参数,或者使用STD::vector > STD::String >如果您正在编写C++代码。
<>祝好运! 你不能在C++中有一个数组作为返回参数
您应该使用char **作为返回参数,或者使用STD::vector > STD::String >如果您正在编写C++代码。
祝你好运!我想你应该在GeTeMes()之前删除“[]”,虽然它不是你要的,它建议你使用<代码> STD::向量<代码> >代码:ST::String < /Cord>s,而不是C++。@ KhairulBasar,它会导致返回值的错误类型。我不认为这是一个“欺骗”。“局部变量的内存是否可以在其作用域之外访问?”。OP的主要问题是关于返回类型,即如何返回字符串数组。他碰巧用局部变量对其进行了说明。@a事实上,标题和主问题是否完全匹配并不重要。标记的dupe有一个可接受的(且高度向上投票的)值回答,这就深入地解释了问题(与OP要求的完全相同)。我认为应该在GetItems()之前删除“[]”虽然不是你所要求的,但它建议你使用<代码> STD::向量 <代码> STD::String < /Cord> s,而不是C++中的.@ KhairulBasar,这会导致返回值的错误类型。我不认为这是一种“局部变量的内存可以在其范围之外访问”的欺骗吗?“。OP的主要问题是关于返回类型,即如何返回字符串数组。他碰巧用一个局部变量对其进行了说明。@a LIGH标题和主要问题是否完全匹配并不重要。标记的dupe有一个可接受的(且高度向上投票)答案,这解释了问题(与OP要求的答案完全相同)深入。我郑重建议bestatic
可能不是个坏主意-编辑。谢谢!我郑重建议bestatic
可能不是个坏主意-编辑。谢谢!警告:不推荐将字符串常量转换为“char*”[-Wwrite strings]<代码> > NG.M.C.GCC-WALLUT/CODE>不产生,但是代码> G++-Wall ;我猜这是一个C++的C++,不是C.@ N.M.YEP,但是它看起来是C类型的。如果他想用C++方式,OP应该使用<代码> STD::向量<代码> >代码:STD::String < /Cord>。t、 将(char*)
放在“Item1”
等之前可以修复警告(我会编辑答案),但这实际上取决于“处理”操作的类型。你不应该