Within C how can I name a function? Just the name.
typedef strlen StringLength
fails to compile with: strlen does not name a type
.
函数指针是一个解决方案
static Integer(*StringLength)(const Byte*) = strlen;
The correct way to "mock" a function - replacing the function call with another call - is to use #define
. If you also don't want to worry about parameters, then simply do:
#define StringLength strlen
Function pointers might work too, but they may be restricted to a certain scope, and you must get the parameter and return types right. For strlen
specifically, the correct type is (C11 7.24.6.3):
size_t strlen(const char *s);
Thus a correctly defined function pointer should look like:
size_t (*const StringLength) (const char*) = strlen;
Where the *const
makes the function pointer itself read-only, which is good practice.
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.