繁体   English   中英

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

[英]How do I call this function with the arguments (void (*store) (int*,int))?

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

#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() {

}

函数is_little_endian只接受一个 neseccary 参数。

这个参数是一个指向函数的指针,它接受指向 int 的指针,然后是 int 并且不返回任何内容 (void)。 你只需要传递一个指向某个函数的指针,就像这样:

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

int main() {
  is_little_endian(example);
}

或者你想要的任何其他功能。 您可以在那里阅读有关函数指针的更多信息: C 中的函数指针如何工作?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM