I have a struct stat variable named s inside a struct I defined myself as follows:
struct myStruct {
struct stat s;
};
and I want to find the difference between the st_mode between two myStruct objects, so my logic is to point to that struct and use a '.' for its member variable.
int func(const void *a, const void *b)
{
return a->s.st_mode - b->s.st_mode;
}
However, there are issues with this implementation:
error: request for member 's' in something not a structure or union
warning: dereferencing 'void *' pointer [enabled by default]
What do I do to correct this?
The parameters to your function are of type const void*
not of type struct myStruct*
.
Do you know for sure that they are in fact pointers to struct myStruct
s? If so you can do a cast:
return ((struct myStruct*)a)->s.st_mode - ((struct myStruct*)b)->s.st_mode
but it's safer to redefine your function to only accept pointers of the correct type thus:
int func(const struct myStruct* a, const struct myStruct* b)
and then keep the rest unchanged.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다