Define functions in structs

No, as functions are not data. But you can define function pointers inside a struct.

struct foo {
    int a;
    void (*workwithit)(struct foo *);
}

You can't really declare stuff inside of a struct in C. This is not C++ or any other OO language where an object encapsulates some kind of scope.

C structs are very simple objects, it's just syntactic sugar for managing a piece of memory. When you create new struct "instance", struct A a;, compiler just reserves stack space according to its size, and when you then do a.member, compiler knows that member's offset, so it jumps to &a + offset of that member. Those offsets are usually not just sums of sizes of preceding members, because compiler usually adds some padding bits into the structure to align it nicer into memory.

Hope it helped a bit. You obviously expect slightly too much from C stuctures :-)


I came to this post because I was looking for a way to teach a bit of Object Oriented "style" of programming in C for a very simple data structures course. I did not want to teach C++ because I did not want to keep explaining its more advanced features.

But I wanted to explore how one might implement the OO pattern used in Python but in a low-level language / run-time. By explaining what is going on in C, students might better understand the Python OO run-time patterns. So I went a bit beyond the first answer above and adapted some of the patterns from https://stackoverflow.com/a/12642862/1994792 but in a way that would elucidate OO run time patterns a bit.

First I made the "class" with a "constructor" in point.c:

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

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

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

void point_del(const struct point* self) {
  free((void *)self);
}

struct point * point_new(int x, int y) {
    struct point *p = malloc(sizeof(*p));
    p->x = x;
    p->y = y;
    p->print = point_print;
    p->del = point_del;
    return p;
}

Then I imported the class, constructed an object from the class, used the object, then destructed the object in main.c

#include "point.c"

int main(void)
{
    struct point * p3 = point_new(4,5);
    p3->print(p3);
    p3->del(p3);
}

It feels very "Pythonic in C".


No, you cannot have functions inside struct in a C program. I wrote a single code and saved that as a .c and a .cpp. The .cpp file complies and works as expected, but the .c file doesn't even compile.

Here is the code for your reference. Save it once as .cpp and then run it. Then save the same code as .c and compile it. You will get a compilation errors.

#include <stdio.h>
struct C {
    void Test(int value) {
       static int var = 0;
       if (var == value) 
          printf("var == value\n");
       else
          printf("var != value\n");

       var = value;
     }
 }; 

 int main() {
    C c1;
    C c2;
    c1.Test(100);
    c2.Test(100);
    int ii;
    scanf("%d",&ii);
 }

No.

You can have function pointers in structs, but that's as close as you'll get.