Can I define a function inside a C structure? [dup

2019-03-08 03:18发布

问题:

This question already has an answer here:

  • Define functions in structs 7 answers

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;

回答1:

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;
}


回答2:

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;
}


回答3:

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: http://forums.devshed.com/c-programming-42/declaring-function-in-structure-in-c-545529.html



回答4:

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.



回答5:

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: 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.



回答6:

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