返回函数指针

扫码查看
for(i = 0; size i; i ++){ printf("%d \ n",a [i]); } } int * func2(void) { static int b [NINE ] = {112,3,4,5,6,7,9,4,10}; 返回b; } / * END new.c * / - pete 默认用户写道: Pradyut Bhattacharya写道: 在这个程序中我无法从指针中检索值 j ....代码: - -------------------------- ------------------------------------------ - ---------------------------------------- #include< stdio.h> void func(int [],int); int * func2(); void main() main()返回int总是。 { int a [] = {3,2,4}; int * j [8]; int i; func(a,2); 数组中的元素数量为3。为什么通过两个? * j = func2(); printf(" \ nTraversing second list \\\"); printf(" ; \ n%d",* j); 哎呀,我刚刚注意到你正在打印一个int指针,好像它是一个int 。还有一个错误。 j是一个int指针数组。可能 你希望它只是一个int指针,这会使printf好。 Brian Hi,in this program i cannot retrive the values from the pointer j....The code :---------------------------------------------------------------------------------------------------------------#include <stdio.h>void func(int [], int);int*func2();void main(){int a[] = {3,2,4};int*j[8];int i;func(a, 2);*j = func2();printf("\nTraversing second list\n");printf("\n%d", *j);}void func(int a[], int size){int i=0;for (i=0; i<=size; i++)printf("%d\n", a[i]);}int*func2(){int b[] = {112,3,4,5,6,7,9,4,10};return *b;}-------------------------------------------------------------------------------------------------------------Any helpThanksPradyut http://pradyut.tkIndia 解决方案 Pradyut Bhattacharya wrote:Hi, in this program i cannot retrive the values from the pointer j....The code :---------------------------------------------------------------------------------------------------------------#include <stdio.h>void func(int [], int);int*func2();void main()main() returns int ALWAYS.{int a[] = {3,2,4};int*j[8];int i;func(a, 2);The number of elements in your array is three. Why pass two?>*j = func2();printf("\nTraversing second list\n");printf("\n%d", *j);You''re obviously not traversing, but accessing the first element.>}void func(int a[], int size){int i=0;for (i=0; i<=size; i++)Change that to i < size printf("%d\n", a[i]);}int*func2(){int b[] = {112,3,4,5,6,7,9,4,10};return *b;Yikes. You pass back an int from a function that''s supposed to bereturning an int *. Presumbably you meant to type:return b;However, that''s not right either. b is a local array, you can NOT useit outside of its scope, which happens to be the function func2().You''ll either have to dynamically allocate an array and populate it, ormake b static.Any helpTurn up the warning level on your compiler.BrianDefault User wrote:>Pradyut Bhattacharya wrote: Hi, in this program i cannot retrive the values from the pointer j.... The code :- ---------------------------------------------------------------------- ---------------------------------------- #include <stdio.h> void func(int [], int); int*func2(); void main()main() returns int ALWAYS. { int a[] = {3,2,4}; int*j[8]; int i; func(a, 2);The number of elements in your array is three. Why pass two? *j = func2(); printf("\nTraversing second list\n"); printf("\n%d", *j);You''re obviously not traversing, but accessing the first element. } void func(int a[], int size) { int i=0; for (i=0; i<=size; i++)Change that to i < size printf("%d\n", a[i]); } int*func2() { int b[] = {112,3,4,5,6,7,9,4,10}; return *b;Yikes. You pass back an int from a function that''s supposed to bereturning an int *. Presumbably you meant to type:return b;However, that''s not right either. b is a local array, you can NOT useit outside of its scope, which happens to be the function func2().You''ll either have to dynamically allocate an arrayand populate it, or make b static. Any helpTurn up the warning level on your compiler./* BEGIN new.c */#include <stdio.h>#define NINE 9void func(int a[], int size);int *func2(void);int main(void){int a[] = {3,2,4};int *(*j)(void) = func2;func(a, sizeof a / sizeof *a);printf("\nTraversing second list\n");func(j(), NINE);return 0;}void func(int a[], int size){int i;for (i = 0; size i; i++) {printf("%d\n", a[i]);}}int *func2(void){static int b[NINE] = {112,3,4,5,6,7,9,4,10};return b;}/* END new.c */--peteDefault User wrote:Pradyut Bhattacharya wrote: Hi, in this program i cannot retrive the values from the pointer j.... The code :- -------------------------------------------------------------------- -- ---------------------------------------- #include <stdio.h> void func(int [], int); int*func2(); void main()main() returns int ALWAYS. { int a[] = {3,2,4}; int*j[8]; int i; func(a, 2);The number of elements in your array is three. Why pass two? *j = func2(); printf("\nTraversing second list\n"); printf("\n%d", *j);Oops, I just noticed that you are printing an int pointer as if it werean int. Yet another mistake. j is an array of int pointers. Possiblyyou wanted it to be just an int pointer, which would make the printf ok.Brian 这篇关于返回函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-28 01:43
查看更多