Can we define functions in structs in C programming language?
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".
You can in C++ instead:
// Example program
#include <iostream>
#include <string>
struct Node
{
int data; Node *prev,*next;
Node(int x, Node* prev=NULL, Node* next=NULL)
{
this->data=x; this->prev=prev; this->next=next;
}
void print_list()
{
Node* temp=this; //temp is created in function call stack
while(temp!=NULL)
{
std::cout<<temp->data<<" ";
temp=temp->next;
}
}
Node* insert_left(int x)
{
Node* temp=new Node(x,this->prev,this);
this->prev=temp;
return temp; //list gets new head
}
Node* insert_right(int x)
{
Node* temp=new Node(x,this,this->next);
this->next=temp;
return this; //this would still be head
}
};
int main()
{
Node* head=new Node(-1); //-1
head=head->insert_left(0); //0 -1
head=head->insert_right(1); //0 1 -1
head=head->insert_left(2); //2 0 1 -1
head->print_list();
}
No.
You can have function pointers in structs, but that's as close as you'll get.
No, You cant define functions inside structures in C programs, However if the extension of your file is .cpp (that is not C), you can have member functions like classes however the default modifier of these functions will be 'public'(unlike class).
Read these links for more information on Structures a good link , another good link, One more good link
As a convention in C++, Classes are used for storing functions and variables both and Structures are used only for storing information (i.e. data).
No, as functions are not data. But you can define function pointers inside a struct.
struct foo {
int a;
void (*workwithit)(struct foo *);
}