Advertisement
Guest User

Untitled

a guest
May 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. -----------------------MYSTRUCT.H---------------------------
  2. #ifndef _MYSTRUCT_H_
  3. #define _MYSTRUCT_H_
  4.  
  5. typedef struct MyStruct {
  6. int myValue1;
  7. char myValue2;
  8. void (*setValue1)(struct MyStruct *myStruct, int myValue1);
  9. void (*setValue2)(struct MyStruct *myStruct, char myValue2);
  10. } MyStruct;
  11.  
  12. struct MyStruct myStructConstructor();
  13. void setValue1(struct MyStruct *myStruct, int myValue1);
  14. void setValue2(struct MyStruct *myStruct, char myValue2);
  15.  
  16.  
  17. #endif
  18.  
  19. --------------------------MYSTRUCT.C-------------------------
  20. #include "MyStruct.h"
  21. #include <stdio.h>
  22.  
  23. struct MyStruct myStructConstructor() {
  24. MyStruct myStruct;
  25.  
  26. myStruct.setValue1 = setValue1;
  27. myStruct.setValue2 = setValue2;
  28. }
  29.  
  30. void setValue1(struct MyStruct *myStruct, int myValue1) {
  31. myStruct->myValue1 = myValue1;
  32. }
  33.  
  34. void setValue2(struct MyStruct *myStruct, char myValue2) {
  35. myStruct->myValue2 = myValue2;
  36. }
  37.  
  38. int main(void) {
  39. MyStruct str = myStructConstructor();
  40.  
  41. str.setValue1(&str, 1);
  42.  
  43. printf("%d\n", str.myValue1);
  44.  
  45. return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement