返回函数指针 [英] returning function pointer

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

问题描述



在这个程序中我无法从指针j中检索值....

代码: -


----------------------------------------------- -------------------------------------------------- -------------


#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(" \\\
Traversing second list \ n");


printf(" \ n%d",* j);


}


void func(int a [],int size)

{

int i = 0;

for (i = 0; i< = size; i ++)

printf("%d \ nn",a [i]);

}


int * func2()

{


int b [] = {112,3,4,5,6, 7,9,4,10};


返回* b;

}


------------------------------------------------ -------------------------------------------------- -----------


任何帮助


谢谢

Pradyut http://pradyut.tk

印度

解决方案

Pradyut Bhattacharya写道:




在这个程序中我无法从指针j中检索值....

代码: -


-------------- -------------------------------------------------- ------

------------------------------------ ----


#include< stdio.h>


void func(int [],int);

int * func2();


void main()



main()返回int ALWAYS。


{

int a [] = {3,2,4};


int * j [8];

int i;


func(a,2);



数组中的元素数量为3。为什么通过两个?


>

* j = func2();

printf(" \\ \\ n遍历第二个列表\ n");


printf(" \ n%d",* j);



你显然没有遍历,而是访问第一个元素。


>

}


void func(int a [],int size)

{

int i = 0;

for(i = 0; i< = size; i ++)



将其更改为i<尺寸


printf("%d \ n",a [i]);

}

int * func2()

{


int b [] = {112,3,4,5,6,7,9 ,4,10};


return * b;



Yikes。你从函数中传回一个int,它应该是

返回一个int *。预定你打算输入:


返回b;


但是,这也不对。 b是一个本地数组,你不能在它的范围之外使用

,它恰好是函数func2()。


你会的或者必须动态分配数组并填充它,或者

使b静止。


任何帮助



调高编译器的警告级别。


Brian


默认用户写道:


>

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(" \\\
Traversing second list\ n");


printf(" \ n%d",* j);



你显然没有遍历,而是访问第一个元素。



}


void func(int a [],int size)

{

int i = 0;

for(i = 0; i< = size; i ++)



将其改为i<尺寸


printf("%d \ n",a [i]);

}

int * func2()

{


int b [] = {112,3,4,5,6,7,9 ,4,10};


return * b;



Yikes。你从函数中传回一个int,它应该是

返回一个int *。预定你打算输入:


返回b;


但是,这也不对。 b是一个本地数组,你不能在它的范围之外使用

,它恰好是函数func2()。


你会的或者必须动态分配一个数组

并填充它,或使b静态。


任何帮助



调高编译器的警告级别。



/ * BEGIN new.c * /


#include< stdio.h>


#define NINE 9

void func(int a [],int size);

int * func2(void);


int main(无效)

{

int a [] = {3,2,4};

int *(* j)(void)= func2;


func(a,sizeof a / sizeof * a);

printf( " \\\
Traversing second list \\\
");

func(j(),NINE);

返回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};


返回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 help

Thanks
Pradyut
http://pradyut.tk
India

解决方案

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 be
returning an int *. Presumbably you meant to type:

return b;

However, that''s not right either. b is a local array, you can NOT use
it outside of its scope, which happens to be the function func2().

You''ll either have to dynamically allocate an array and populate it, or
make b static.

Any help

Turn up the warning level on your compiler.


Brian


Default 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 be
returning an int *. Presumbably you meant to type:

return b;

However, that''s not right either. b is a local array, you can NOT use
it outside of its scope, which happens to be the function func2().

You''ll either have to dynamically allocate an array
and populate it, or make b static.

Any help


Turn up the warning level on your compiler.


/* BEGIN new.c */

#include <stdio.h>

#define NINE 9

void 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 */
--
pete


Default 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 were
an int. Yet another mistake. j is an array of int pointers. Possibly
you wanted it to be just an int pointer, which would make the printf ok.


Brian


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

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