如何使用参数(void(* store)(int *,int))调用此函数?

时间:2018-09-02 00:22:28

标签: c

我是c语言的新手,对指针或如何使用这些参数在main中调用此方法的设置并不真正熟悉。我对指针雪有一点了解,但是我仍然对方法参数中的一个感到困惑。我要传递一个指针和一个int吗?我是否需要传递任何内容?我甚至需要main方法还是可以将is_little_endian作为主要方法运行程序?

#include "test_endian.h"
#include <stdio.h>

int is_little_endian(void (*store)(int*,int)) {
  int x;

  unsigned char *byte_ptr = (unsigned char*)(&x);

  store(&x, 1);

  printf("the address for x is %u\n", *byte_ptr);

  return 0;
}


int main() {

}

1 个答案:

答案 0 :(得分:0)

函数is_little_endian仅接受一个必需的参数。

此参数是指向函数的指针,该函数接受指向int的指针,然后接受int且不返回任何内容(无效)。您只需要在此处传递指向某个函数的指针,就像这样:

void example(int * a, int b) { }

int main() {
  is_little_endian(example);
}

或您想要的任何其他功能。您可以在此处了解有关函数指针的更多信息:How do function pointers in C work?

是的,您需要运行程序的主要方法,就像您的身体需要心脏一样。 ;)

相关问题