66

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;
DCuser
  • 953
  • 2
  • 10
  • 21

6 Answers6

93

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;
}
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • 1
    What is `point*` means? I mean, what is the meaning of putting * after the variable name? – explorer Jun 15 '20 at 23:22
  • 'point' is the type name of the struct. 'p' is the variable. – Rathnavel Oct 09 '20 at 06:50
  • 1
    @explorer to be precise, `point* p` means passing a pointer to the struct as an argument. This way, the memory is saved (passing as value creates a copy of the variable on the stack). `point*` signifies it is a pointer to the struct `p` as opposed to just `point` which signifies it is a struct and will be passed as a copy of the original struct. – Parth Sarthi Sharma Jul 23 '21 at 05:25
21

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;
}
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
9

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.

epsalon
  • 2,294
  • 12
  • 19
8

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

L. Miller
  • 347
  • 1
  • 2
  • 6
Salgar
  • 7,687
  • 1
  • 25
  • 39
3

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.

L. Miller
  • 347
  • 1
  • 2
  • 6
shanet
  • 7,246
  • 3
  • 34
  • 46
1

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

Fantasy Shao
  • 615
  • 4
  • 8