简体   繁体   中英

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

I'm new to c and not really familiar with pointers or how this method is setup to be called in main with these arguments. I have a bit of an understanding of pointer snow, but i'm still confused with one being in the method arguments. Do I pass in a pointer and an int? Do I need to pass in anything at all? Do I even need the main method or can I just run the program with is_little_endian as my main method?

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

}

Function is_little_endian accepts only one parameter which is neseccary.

This parameter is a pointer to a function, which accepts pointer to int, then int and returns nothing (void). You just need to pass there a pointer to some function, like that:

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

int main() {
  is_little_endian(example);
}

Or any other function you wish. You can read more about pointers to function there: How do function pointers in C work?

And yes, you need the main method to run the program, like your body needs your heart. ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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