Accessing struct member variables passed into function

maregor

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?

wxs

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 myStructs? 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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Segfault accessing or setting struct member

분류에서Dev

Accessing a member function through an object

분류에서Dev

angular $interval - accessing variables outside of the function

분류에서Dev

accessing double pointer 2d array passed by reference to a function

분류에서Dev

C# struct variables remain null when passed into unmanaged C DLL functions

분류에서Dev

Accessing struct from double pointer

분류에서Dev

$_POST variables not passed by form

분류에서Dev

Accessing Environment Variables in flash

분류에서Dev

Accessing Static variables in cakephp

분류에서Dev

C - Function definition has pointers for arguments, is passed variables not pointers when called, but compiles and runs (sometimes). How?

분류에서Dev

Accessing private static member through an object instance

분류에서Dev

accessing the member of a class of pointer array of another class

분류에서Dev

Accessing protected member's public method

분류에서Dev

Accessing nested hashes using variables

분류에서Dev

Accessing timeline variables from a class?

분류에서Dev

Accessing nested JavaScript objects with variables

분류에서Dev

Accessing function of iframe

분류에서Dev

What is better practise in high-performance computing: passing a struct of data into a function or a set of variables?

분류에서Dev

Why is offsetof(member) equal to sizeof(struct)?

분류에서Dev

C - Help printing a member of a returned Struct

분류에서Dev

Malloc'ing pointer-to-pointer member of struct

분류에서Dev

In a member function, can this == NULL?

분류에서Dev

Lambda function as class member

분류에서Dev

Pointer to array of struct in function

분류에서Dev

accessing json data passed from php (json_encode(databaseresults))

분류에서Dev

Accessing object properties in another function

분류에서Dev

Accessing javascript variables across html files

분류에서Dev

Javascript accessing class variables from window object

분류에서Dev

Accessing the variables of a parent widget via a button

Related 관련 기사

  1. 1

    Segfault accessing or setting struct member

  2. 2

    Accessing a member function through an object

  3. 3

    angular $interval - accessing variables outside of the function

  4. 4

    accessing double pointer 2d array passed by reference to a function

  5. 5

    C# struct variables remain null when passed into unmanaged C DLL functions

  6. 6

    Accessing struct from double pointer

  7. 7

    $_POST variables not passed by form

  8. 8

    Accessing Environment Variables in flash

  9. 9

    Accessing Static variables in cakephp

  10. 10

    C - Function definition has pointers for arguments, is passed variables not pointers when called, but compiles and runs (sometimes). How?

  11. 11

    Accessing private static member through an object instance

  12. 12

    accessing the member of a class of pointer array of another class

  13. 13

    Accessing protected member's public method

  14. 14

    Accessing nested hashes using variables

  15. 15

    Accessing timeline variables from a class?

  16. 16

    Accessing nested JavaScript objects with variables

  17. 17

    Accessing function of iframe

  18. 18

    What is better practise in high-performance computing: passing a struct of data into a function or a set of variables?

  19. 19

    Why is offsetof(member) equal to sizeof(struct)?

  20. 20

    C - Help printing a member of a returned Struct

  21. 21

    Malloc'ing pointer-to-pointer member of struct

  22. 22

    In a member function, can this == NULL?

  23. 23

    Lambda function as class member

  24. 24

    Pointer to array of struct in function

  25. 25

    accessing json data passed from php (json_encode(databaseresults))

  26. 26

    Accessing object properties in another function

  27. 27

    Accessing javascript variables across html files

  28. 28

    Javascript accessing class variables from window object

  29. 29

    Accessing the variables of a parent widget via a button

뜨겁다태그

보관