Functions as members of structures

As another Javascript developer learning C I am trying to implement a basic object.

UserStruct.h

#ifndef USERSTRUCT_H
#define USERSTRUCT_H

struct User {
    int age;
    int (* getAge)();
};
typedef struct User User;

int getAge(User);
User initUser(int);
#endif /* USERSTRUCT_H */

      

UserStruct.c

#include "./UserStruct.h"
int getAge(User this) {
  return this.age;
} 

User initUser(int age){  
  User user;
  user.age = age;
  user.getAge = getAge;
  return user;
}

      

main.c

#include <stdio.h>
#include <stdlib.h>
#include "./UserStruct.h"
int main(int argc, char** argv) {
   User user = initUser(15); // sets age to 15..
   user.age = 2222;
   printf("%d\n",user.getAge(1234)); // note the int passing here..
   printf("%d\n",user.age);
  return (EXIT_SUCCESS);
}

      

Questions !:
1. Can anyone explain the syntax int (* getAge)();

inside the UserStruct definition?
2. getAge expects to be passed to it User

, how does it work with the transfer int

? This is especially odd since the getAge implementation uses return this.age

.

While I was figuring out how to fix this, I'm still not sure why this behaves the way it does. (The solution is to pass the user pointer to getAge)

+3


source to share


2 answers


C is not an object oriented language as it existed before the object, but your example demonstrates that it can be used with objects.

  • int (* getAge)()

    is a function pointer, the function takes any parameters ()

    and returns an int, to indicate that the function does not take a parameter, must be defined(void)

  • getAge - this "method" for the user class that needs an instance of an object in the object language syntax will be user.getAge()

    , which is implicitly transmitted this

    as the first argument, explicitly in C: getAge(user)

    .



The problem is that int can be used as a pointer, which means it user.getAge(1234)

will be used 1234

as an address for the user, so it will take the second int field as the address for the fuction getAge and jump to that address.

+3


source


I have changed your code to work better.

The getAge

structure field User

is a pointer to a function. I changed my prototype to get it specifies a function that gets a pointer to the struct from which you want to get the field contents age

. (I used a pointer to a structure to avoid carrying the entire structure with the stack)



#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdlib.h>

struct User {
    int age;
    int (* getAge)(struct User *);
};

typedef struct User User;

int getAge(User *);
User initUser(int);

int getAge(User * this_) {
  return this_->age;
}

User initUser(int age){
  User user;
  user.age = age;
  user.getAge = getAge;
  return user;
}

int main(int argc, char** argv) {
   User user = initUser(15); // sets age to 15..

   // user.age = 2222;

   printf("%d\n",user.getAge(&user)); // note the pointer used as parameter

   printf("%d\n",user.age);
  return (EXIT_SUCCESS);
}

      

+2


source







All Articles