我在回答C中的函数指针如何工作时看到了这个示例代码?
#import <stdlib.h>
#define MAX_COLORS 256
typedef struct {
char* name;
int red;
int green;
int blue;
} Color;
Color Colors[MAX_COLORS];
void eachColor (void (*fp)(Color *c)) {
int i;
for (i=0; i<MAX_COLORS; i++)
(*fp)(&Colors[i]);
}
void printColor(Color* c) {
if (c->name)
printf("%s = %i,%i,%i\n", c->name, c->red, c->green, c->blue);
}
int main() {
Colors[0].name="red";
Colors[0].red=255;
Colors[1].name="blue";
Colors[1].blue=255;
Colors[2].name="black";
eachColor(printColor);
}
Run Code Online (Sandbox Code Playgroud)
该代码返回以下错误:
test.c: In function ‘printColor’:
test.c:21: warning: incompatible implicit declaration of built-in function ‘printf’
Run Code Online (Sandbox Code Playgroud) 在一本书中,我遇到了以下问题.我面临的问题是,什么是函数指针?它是如何工作的以及它声明的语法是什么.
将错误指向给定代码.
main()
{
int (*p)()=fun;
(*p)();
}
fun()
{
printf("Hi..");
}
Run Code Online (Sandbox Code Playgroud)
我的第一个问题是,下面的代码snipet表示什么?
int (*p)()=fun;
(*p)();
Run Code Online (Sandbox Code Playgroud)
第二个明显的问题是给定代码中的错误是什么.??
请参阅ionela.voinescu的解决方案答案..它与解决方案手册中的内容相同
如何声明f作为指向函数的指针(指向指向整数的指针,双精度)以返回指向整数的指针?
我尝试了很多变种,包括:
(int**)(f*)(int**,double)
(**int)(f*)(int**,double)
*(int)(f*)(int**,double)
Run Code Online (Sandbox Code Playgroud) 考虑我设法编写的以下代码:
#include <stdio.h>
#define FOR(A, B, C) for(A; B; C++)
int main()
{
FOR(i=0, i<10, i)
printf("%i", i);
return 1;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
0123456789
如果我这样做,FOR(i=5, i<10, i)
那么输出分别是56789
我的问题是合法的吗?它会在不同的情况下导致任何错误吗?它是否像for循环一样工作?
是否有充分的理由将函数的指针传递给C中的另一个函数.我没有看到函数指针的一般用例.
如果有人可以给出一些用例,其中函数的指针是实现某些特定用例的唯一方法,这将是很好的.
这个c代码做了什么?
{
int (*func)();
func = (int (*)()) code;
(int)(*func)();
}
Run Code Online (Sandbox Code Playgroud)
特别是我对subj感到困惑.
我正在考虑以下两者之间的区别:
void *signal(int, void (*)(int))(int)
Run Code Online (Sandbox Code Playgroud)
和
void (*signal(int, void (*)(int)))(int)
Run Code Online (Sandbox Code Playgroud)
我知道后者来自这里 - 示例#3:"终极"(当我试着大声说出来理解它时,这是一次热闹的学习经历):
signal是一个函数(int, void (*)(int))作为输入并返回一个指向另一个获取(int)和返回的函数的指针void.
对于前者我认为,因为最后一个(int)会有更高的优先级,*所以它应该是语法错误,但是从cdecl.org,结果是:
["]声明信号为函数(int,指向函数的函数(int)返回void)返回函数(int)返回指向void [."]的指针
所以我需要一张支票.
我是 C++ 的初学者,我在理解函数指针时遇到了问题:
main()
{
double pam(int);
double (*pf)(int);
pf = pam;
double x = pam(4);
double y = (*pf)(5);
double y = pf(5);
}
Run Code Online (Sandbox Code Playgroud)
怎么可能(*pf)()并且pf()是一样的,pf指向函数的指针在哪里?
其他指针和指向函数的指针有什么区别??
我正在尝试 C 中的回调函数,但我不确定为什么,但出于某种原因 printf 在回调函数中不起作用。例如:
#include <stdio.h>
void call_this_method()
{
printf("call_this_method called\n");
}
void callback(void (*method))
{
printf("callback called\n");
(void) method;
}
int main(int argc, const char * argv[])
{
callback(call_this_method);
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试运行它,只有“回调调用”会在控制台中打印出来;“call_this_method called”不会被打印出来。这是为什么?
我很困惑C,因为我是新手.我知道1.1给了我最大值,1.2给了我最大值变量地址[ 图片 ]. 我的问题是如何在main中调用*findmax函数?
int * findMax(int *a,int SIZE){
int i,max=*a,address,add;
for(i=0;i<SIZE;i++){
if(max<*(a+i)){
max=*(a+i);
}
}
//printf("maxium value is %d at index %x",max,&max);
return &max;
}
Run Code Online (Sandbox Code Playgroud)