Pointers 指向c样式字符串c++/cli
我找不到这个问题的原因 函数getfruits返回指向c样式字符串指针数组的指针。 Main尝试访问c样式的字符串Pointers 指向c样式字符串c++/cli,pointers,c++-cli,cstring,Pointers,C++ Cli,Cstring,我找不到这个问题的原因 函数getfruits返回指向c样式字符串指针数组的指针。 Main尝试访问c样式的字符串 using namespace System; #pragma managed void getfruits(char ***list, int* count) { char *txt[] = { "apple", "orange", "pears", "banana", }; *l
using namespace System;
#pragma managed
void getfruits(char ***list, int* count)
{
char *txt[] =
{
"apple",
"orange",
"pears",
"banana",
};
*list = txt;
*count = 4;
}
#pragma managed
int main(array<System::String ^> ^args)
{
char **lst; int cnt;
getfruits(&lst,&cnt);
char *t; int i;
String^ s;
for (i=0; i<cnt; i++)
{
t = lst[i]; //t = <undefined value>
s = gcnew String(t);
Console::WriteLine("Fruit = {0}", s);
};
Console::ReadKey();
return 0;
}
使用名称空间系统;
#布拉格管理
void getfruits(字符***列表,整数*计数)
{
char*txt[]=
{
“苹果”,
“橙色”,
“梨”,
“香蕉”,
};
*list=txt;
*计数=4;
}
#布拉格管理
int main(数组^args)
{
字符**lst;int cnt;
getfruits(第一、第二和第三);
字符*t;整数i;
字符串^s;
对于(i=0;i您返回了一个本地数组的地址并使用了它。也就是说,txt
具有自动存储持续时间,并且在您离开getfruit
函数时会被销毁。但是您保留了一个指向它的指针并使用它,但是指向的内存不再包含一个有生命的对象。我建议您或许应该阅读一个。 您已经返回了本地数组的地址并使用了它。也就是说,txt
具有自动存储持续时间,并且在您离开getfruit
函数时会被销毁。但是您保留了指向它的指针并使用了它,但是指向的内存不再包含活动对象。我建议您或许应该阅读一篇文章