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 seemseasy, but I keep running into the same problem. The function appearsto execute properly, but when I print out the array values, only thefirst one is correct. The other are all wrong. A snippet of code isbelow. Please take a look and see what I have done wrong. Thank youin advance for your help.void main(void)int *point(int); // function protoint *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 functionreturn 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 ofan array (that is, what you get when you use an array name in an expressionwhere it is not the operand of & or sizeof) is a pointer to its firstelement. 传递指向数组的指针。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 differencebetween T * and T (*)[N].And since the OP''s program didn''t even attempt to pass an array, I don''t seewhich 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.ukemail: rjh at the above domain, - www. 这篇关于函数返回指针问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-28 01:43
查看更多