容易指向函数问题 [英] easy pointers to functions problem

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

问题描述




我想知道函数指针是如何工作的。

我有以下内容:

#include< stdio.h>


void do_stuff(int *,int,void *);

void getInt(int * );

void showInt(int *);


int main(){


int num_array [10 ];


do_stuff(num_array,10,getInt);

do_stuff(num_array,10,showInt);


return(0);

}


void do_stuff(int * a,int size,void(* process)(int *)){

int i;

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

process(& a [i]);

}

}


void getInt(int * ptr){

if((scanf) ("%d",ptr))!= 1){

printf(" error");


}

}


void showInt(int * ptr){

printf("%d",* ptr);

}


但我得到错误为do_stuff混淆了类型还有警告ISO C

禁止在指针和空格之间传递arg3的do_stuff *


我在这里无处可去。有人可以帮我解决我的问题吗?b $ b错了吗?


谢谢


Michael

Hi,

I am trying to get an idea of how function pointers work.
I have the following:

#include <stdio.h>

void do_stuff(int*,int,void*);
void getInt(int*);
void showInt(int*);

int main(){

int num_array[10];

do_stuff(num_array, 10, getInt);
do_stuff(num_array, 10, showInt);

return(0);
}

void do_stuff(int *a, int size, void (*process)(int *)){
int i;
for(i = 0; i < size; i++){
process(&a[i]);
}
}

void getInt(int *ptr){
if((scanf("%d", ptr)) != 1){
printf("error");

}
}

void showInt(int *ptr){
printf("%d", *ptr);
}

but I get errors "conficting types for do_stuff" and also warnings "ISO C
forbids passing of arg3 of do_stuff between pointer and void *"

I''m getting nowhere fast here. Can someone help me out with what I have
wrong?

Thanks

Michael

推荐答案



Michael写道:

Michael wrote:

#include< stdio.h> ;


void do_stuff(int *,int,void *);
#include <stdio.h>

void do_stuff(int*,int,void*);



void do_stuff(int * a,int size,void(* process)(int *));


< ; snip>

void do_stuff(int *a, int size, void (*process)(int *));

<snip>


void do_stuff(int * a,int size,void(* process)(int *)){

int i ;
void do_stuff(int *a, int size, void (*process)(int *)){
int i;



< snip>

<snip>


Michael写道:
Michael wrote:




我想知道函数指针是如何工作的。

我有以下内容:


#include< stdio.h>


void do_stuff(int *,int,void *);
Hi,

I am trying to get an idea of how function pointers work.
I have the following:

#include <stdio.h>

void do_stuff(int*,int,void*);



这里do_stuff的最后一个参数是void *。 void * is * not * compatible

带有函数指针,只有对象指针(即指向数据的指针)。

I.e.你不能将函数指针作为void *参数传递。

Here the last parameter of do_stuff is void*. void* is *not* compatible
with function pointers, only object pointers (i.e. pointers to data).
I.e. you cannot pass a function pointer as a void* parameter.


void getInt(int *);

void showInt(int *);


int main(){
void getInt(int*);
void showInt(int*);

int main(){



如果你没有使用参数,最好是明确的。


int main(void){

If you are not using the parameters, it is better to be explicit.

int main(void) {


int num_array [10];


do_stuff(num_array,10,getInt);

do_stuff(num_array,10,showInt);
int num_array[10];

do_stuff(num_array, 10, getInt);
do_stuff(num_array, 10, showInt);



为什么神奇数字10?我会使用#define作为数组大小,然后

只有一个地方可以更改它。

Why the magic number 10? I would use a #define for the array size, then
there is only one place to change it.


return(0);

}


void do_stuff(int * a,int size,void(* process)(int *)){
return(0);
}

void do_stuff(int *a, int size, void (*process)(int *)){



这与上面的do_stuff原型不同,就像

编译器告诉你的那样!这是正确的,所以改变你原来的

原型来匹配。

This is different to your prototype of do_stuff above, just as the
compiler told you! It is this that is correct, so change your earlier
prototype to match.


int i;

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

process(& a [i]);

}

}


void getInt(int * ptr){

if((scanf("%d",ptr))!= 1){

printf(" error");
int i;
for(i = 0; i < size; i++){
process(&a[i]);
}
}

void getInt(int *ptr){
if((scanf("%d", ptr)) != 1){
printf("error");



您可能希望在printf的末尾有一个/ n。或者使用puts。

You might want a /n at the end of that printf. Or use puts.


}

}


void showInt(int * ptr){

printf("%d",* ptr);

}


但是我收到错误混淆了类型do_stuff"
}
}

void showInt(int *ptr){
printf("%d", *ptr);
}

but I get errors "conficting types for do_stuff"



上述错误很明显。你的原型和定义不匹配。

这有点像你告诉别人你会给他们一个橙子和

然后给他们一个板球棒,当然他们会告诉你他们

是不同的!

The above error is obvious. Your prototype and definition don''t match.
It''s a bit like you telling someone you will give them an orange and
then giving them a cricket bat, of course they will tell you that they
are different!


以及警告ISO C

禁止在指针和void之间传递do_stuff的arg3 *
and also warnings "ISO C
forbids passing of arg3 of do_stuff between pointer and void *"



这不太明显。 void *只是一个指向对象的通用指针(即

数据),它不能指向函数。想象一下具有32位数据的系统

指针和64位函数指针。 64不做到32!

-

Flash Gordon,生活在有趣的时代。

网站 - http://home.flash-gordon.me.uk/

comp.lang.c发布指南和介绍:
http:// clc-wiki.net/wiki/Intro_to_clc




Flash Gordon写道:

Flash Gordon wrote:

Michael写道:
Michael wrote:

printf(" error");
printf("error");



你可能想要在printf的末尾加一个/ n。或使用看跌期权。


You might want a /n at the end of that printf. Or use puts.



你指的是''\ n''。

Do you point to a ''\n''.


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

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