函数返回指针问题 [英] function to return pointer question

查看:84
本文介绍了函数返回指针问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我一直在做一个我需要有一个功能的程序

返回一个数组。我发现C不会这样做,所以现在我想要让函数返回指向数组的指针。
好像很容易,但我一直遇到同样的问题。函数出现

来正确执行,但是当我打印出数组值时,只有

第一个是正确的。其他都错了。一段代码是

以下。请看看我做错了什么。谢谢

提前帮助。


void main(无效)

int * point(int); //功能原型

int * p,i,count;

i = 0;

count = 5;

p = point(count); //调用函数,p应该是数组x2的值[0]


for(i = 0; i< count; i ++)//打印出数组


{

printf(&\\; \ n \\ n函数retun =%i",* p);

p ++;

} $ / $
}

int * point(int k)//创建数组k-1整数的函数。

{

int * p2,x2 [k],i2;

i2 = 0;

while(i2< k)

{

x2 [i2] = 3 * k;

i2 ++;

}

p2 = x2;

返回p2;

}

Hello,
I have been working on a program where I need to have a function
return an array. I found out that C doesn''t do this, so now I am
trying to get the function to return a pointer to an array. It seems
easy, but I keep running into the same problem. The function appears
to execute properly, but when I print out the array values, only the
first one is correct. The other are all wrong. A snippet of code is
below. Please take a look and see what I have done wrong. Thank you
in advance for your help.

void main(void)
int *point(int); // function proto
int *p, i,count;
i=0;
count=5;

p=point(count); // call function, p should be value at array x2[0]

for(i=0;i<count;i++) // print out array

{
printf("\nfunction retun = %i",*p);
p++;
}
}
int *point(int k) // function to create array k-1 integers long.
{
int *p2,x2[k],i2;
i2=0;
while(i2<k)
{
x2[i2]=3*k;
i2++;
}
p2=x2;
return p2;
}

推荐答案

Andrew Gentile写道:
Andrew Gentile wrote:

你好,

我一直在做一个我需要一个函数的程序

返回一个数组。我发现C不会这样做,所以现在我想要让函数返回指向数组的指针。
好像很容易,但我一直遇到同样的问题。函数出现

来正确执行,但是当我打印出数组值时,只有

第一个是正确的。其他都错了。一段代码是

以下。请看看我做错了什么。谢谢

提前为您提供帮助。
Hello,
I have been working on a program where I need to have a function
return an array. I found out that C doesn''t do this, so now I am
trying to get the function to return a pointer to an array. It seems
easy, but I keep running into the same problem. The function appears
to execute properly, but when I print out the array values, only the
first one is correct. The other are all wrong. A snippet of code is
below. Please take a look and see what I have done wrong. Thank you
in advance for your help.



将来,请发布EXACT代码。剪切和粘贴。我可以告诉你

并不是因为这一切搞砸了并且没有编译过。

In the future, please post EXACT code. Cut and paste. I can tell you
didn''t because this is all screwed up and wouldn''t have compiled.


void main(void )
void main(void)



main()返回int,ALWAYS。你也错过了打开大括号(和

它会出现的结束括号。

main() returns int, ALWAYS. You''re also missing then opening brace (and
the closing braces it would appear.


int * point(int); / / function proto

int * p,i,count;

i = 0;

count = 5;


p = point(count); //调用函数,p应该是数组x2的值[0]


for(i = 0; i< count; i ++)/ /打印出来的数组


{

printf(&\\; \ nn \\ n函数重新=%i",* p);

p ++;

}

}


int * point(int k)//创建数组的函数k-1整数长。

{

int * p2,x2 [k],i2;

i2 = 0;

而(i2< k)

{

x2 [i2] = 3 * k;

i2 ++;

}

p2 = x2;
int *point(int); // function proto
int *p, i,count;
i=0;
count=5;

p=point(count); // call function, p should be value at array x2[0]

for(i=0;i<count;i++) // print out array

{
printf("\nfunction retun = %i",*p);
p++;
}
}
int *point(int k) // function to create array k-1 integers long.
{
int *p2,x2[k],i2;
i2=0;
while(i2<k)
{
x2[i2]=3*k;
i2++;
}
p2=x2;



你已经设置p2指向自动数组的开始,这将是
函数返回时
超出范围。你不能这样做。

You''ve set p2 to point to the start of an automatic array, which will
go out of scope as soon as the function returns. You can''t do that.


返回p2;

}
return p2;
}



使用malloc()或<在point()中动态分配数组br />
朋友,或从调用函数传入数组。


Brian

Either dynamically allocate the array in point() using malloc() or
friends, or pass in the array from the calling function.

Brian


" Andrew泰尔" < a ************ @ gmail.comwrites:
"Andrew Gentile" <an************@gmail.comwrites:

您好,

我一直在工作在我需要有一个函数的程序上

返回一个数组。我发现C不会这样做,所以现在我是
Hello,
I have been working on a program where I need to have a function
return an array. I found out that C doesn''t do this, so now I am



唯一没有严格按值传递的类型。传递给数组的指针是

。它是C的东西。


关于你的问题:在
函数点()中查看你的局部变量x2。当您从声明它的函数返回

时,本地将被有效销毁。


查看使用malloc。

$祝b $ b好​​运!

The only type not strictly passed by value. A pointer to the array is
passed. Its a C thing.

With regards to your problem : Look at your local variable x2 in
function point(). A local will be effectively destroyed when you return
from the function which declared it.

Look into using malloc.

best of luck!


试图让函数返回指向数组的指针。好像很容易,但我一直遇到同样的问题。函数出现

来正确执行,但是当我打印出数组值时,只有

第一个是正确的。其他都错了。一段代码是

以下。请看看我做错了什么。谢谢

提前帮助。


void main(无效)

int * point(int); //功能原型

int * p,i,count;

i = 0;

count = 5;

p = point(count); //调用函数,p应该是数组x2的值[0]


for(i = 0; i< count; i ++)//打印出数组


{

printf(&\\; \ n \\ n函数retun =%i",* p);

p ++;

}

}


int * point(int k)//创建数组k-1整数的函数。

{

int * p2,x2 [k],i2;

i2 = 0;

while(i2< k)

{

x2 [i2] = 3 * k;

i2 ++;

}

p2 = x2;

返回p2;

}
trying to get the function to return a pointer to an array. It seems
easy, but I keep running into the same problem. The function appears
to execute properly, but when I print out the array values, only the
first one is correct. The other are all wrong. A snippet of code is
below. Please take a look and see what I have done wrong. Thank you
in advance for your help.

void main(void)
int *point(int); // function proto
int *p, i,count;
i=0;
count=5;

p=point(count); // call function, p should be value at array x2[0]

for(i=0;i<count;i++) // print out array

{
printf("\nfunction retun = %i",*p);
p++;
}
}
int *point(int k) // function to create array k-1 integers long.
{
int *p2,x2[k],i2;
i2=0;
while(i2<k)
{
x2[i2]=3*k;
i2++;
}
p2=x2;
return p2;
}



-

--


理查德说:
Richard said:

" Andrew Gentile" < an ************ @ gmail.comwrites:
"Andrew Gentile" <an************@gmail.comwrites:

>你好,
我一直在努力我需要有一个函数的程序
返回一个数组。我发现C不会这样做,所以现在我是
>Hello,
I have been working on a program where I need to have a function
return an array. I found out that C doesn''t do this, so now I am



唯一没有严格按值传递的类型。


The only type not strictly passed by value.



类型未通过。传递值,值具有类型。

一个数组的值(也就是说,当你在表达式中使用数组名称时得到的结果

,它不是&或sizeof的操作数)指向其第一个

元素的指针。

Types aren''t passed. Values are passed, and values have types. The value of
an array (that is, what you get when you use an array name in an expression
where it is not the operand of & or sizeof) is a pointer to its first
element.


传递指向数组的指针。
A pointer to the array is passed.



否,传递一个指向数组第一个元素的指针。这就是T *和T(*)[N]之间的差异




因为OP的程序甚至都没有尝试过通过一个阵列,我没有看到

,确切地说,你没有做到这一点。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

No, a pointer to the array''s first element is passed. This is the difference
between T * and T (*)[N].

And since the OP''s program didn''t even attempt to pass an array, I don''t see
which point it is, precisely, that you are failing to make.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


这篇关于函数返回指针问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆