1

I'm newly learned about function pointers here but I couldn't define a function pointer to be32toh() or other functions of endian.h header.

First of all, I can do what is told in the given thread:


#include <endian.h>
int addInt(int n, int m) {
    return n+m;
}
int main(){
    int (*functionPtr)(int,int);
    functionPtr = addInt;    
    return 0;
}

But when I try to do the same for a function like be32toh(), I'll get a compilation error:

#include <stdint.h>
#include <endian.h>
int addInt(int n, int m) {
    return n+m;
}
int main(){
    int (*functionPtr)(int,int);
    functionPtr = addInt;
    uint32_t (*newptr)(uint32_t);
    newptr = &be32toh;
    return 0;
}

Compile as:

$ gcc test.c

Result is as below:

test.c: In function ‘main’:
test.c:15:15: error: ‘be32toh’ undeclared (first use in this function)
   15 |     newptr = &be32toh;
      |               ^~~~~~~
test.c:15:15: note: each undeclared identifier is reported only once for each function it appears in

What's the problem and how to fix it?

milad
  • 1,854
  • 2
  • 11
  • 27

1 Answers1

2

What's the problem

be32toh is a macro.

how to fix it?

Just write the function yourself.

uint32_t be32toh_func(uint32_t a) {
     return be32toh(a);
}

....
     newptr = &be32toh_func;
KamilCuk
  • 120,984
  • 8
  • 59
  • 111