Can I define a function inside a C structure?

CFunctionStructure

C Problem Overview


I am trying to convert some C++ code to C and I am facing some problems. How can I define inside a structure a function?

Like this:

 typedef struct  {
    double x, y, z;
    struct Point *next;
    struct Point *prev;
    void act() {sth. to do here};
} Point;

C Solutions


Solution 1 - C

No, you cannot define a function within a struct in C.

You can have a function pointer in a struct though but having a function pointer is very different from a member function in C++, namely there is no implicit this pointer to the containing struct instance.

Contrived example (online demo http://ideone.com/kyHlQ):

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct point
{
    int x;
    int y;
    void (*print)(const struct point*);
};

void print_x(const struct point* p)
{
    printf("x=%d\n", p->x);
}

void print_y(const struct point* p)
{
    printf("y=%d\n", p->y);
}

int main(void)
{
    struct point p1 = { 2, 4, print_x };
    struct point p2 = { 7, 1, print_y };

    p1.print(&p1);
    p2.print(&p2);

    return 0;
}

Solution 2 - C

You can have a function pointer in a struct though. but not in this way

you can define it in this way

example:

typedef struct cont_func 
{
    int var1;
    int (*func)(int x, int y);
    void *input;
} cont_func;


int max (int x, int y)
{
    return (x > y) ? x : y;
}

int main () {
   struct cont_func T;

   T.func = max;
}

Solution 3 - C

In C it is not allowed to define a method inside a struct. You could define a function pointer inside a struct as follows:

typedef struct  {
  double x, y, z;
  struct Point *next;
  struct Point *prev;
  void (*act)();
} Point;

You will have to assign the pointer to a specific function whenever you instantiate the struct.

Solution 4 - C

No, it is not possible to declare a function inside a struct in C.

That is (one of) the fundamental differences between C and C++.

See this thread: https://web.archive.org/web/20121024233849/http://forums.devshed.com/c-programming-42/declaring-function-in-structure-in-c-545529.html

Solution 5 - C

The idea is to put a pointer to a function inside the struct. The function is then declared outside of the struct. This is different from a class in C++ where a function is declared inside the class.

For example: stealing code from here: https://web.archive.org/web/20121024233849/http://forums.devshed.com/c-programming-42/declaring-function-in-structure-in-c-545529.html

struct t {
    int a;
    void (*fun) (int * a);
} ;

void get_a (int * a) {
    printf (" input : ");
    scanf ("%d", a);
}

int main () {
    struct t test;
    test.a = 0;

    printf ("a (before): %d\n", test.a);
    test.fun = get_a;
    test.fun(&test.a);
    printf ("a (after ): %d\n", test.a);

    return 0;
}

where test.fun = get_a; assigns the function to the pointer in the struct, and test.fun(&test.a); calls it.

Solution 6 - C

You can only define a function pointer in a struct in C programming language which is different from C++.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDCuserView Question on Stackoverflow
Solution 1 - ChmjdView Answer on Stackoverflow
Solution 2 - CMOHAMEDView Answer on Stackoverflow
Solution 3 - CepsalonView Answer on Stackoverflow
Solution 4 - CSalgarView Answer on Stackoverflow
Solution 5 - CshanetView Answer on Stackoverflow
Solution 6 - CFantasy ShaoView Answer on Stackoverflow