簡體   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