From 70106fd5e0540300d2262caa8df546828cab9825 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 1 May 2021 18:36:52 +0100 Subject: add methods in c post --- .../gemtext/gemfeed/2016-11-20-methods-in-c.gmi | 76 ++++++++++++++++++++++ content/gemtext/gemfeed/atom.xml | 13 +++- 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 content/gemtext/gemfeed/2016-11-20-methods-in-c.gmi (limited to 'content/gemtext/gemfeed') diff --git a/content/gemtext/gemfeed/2016-11-20-methods-in-c.gmi b/content/gemtext/gemfeed/2016-11-20-methods-in-c.gmi new file mode 100644 index 00000000..88315d23 --- /dev/null +++ b/content/gemtext/gemfeed/2016-11-20-methods-in-c.gmi @@ -0,0 +1,76 @@ +> Written by Paul Buetow 2016-11-20 + +=> ../ Go back to the main site + +# Methods in C + +You can do some sort of object oriented programming in the C Programming Language. However, that is very limited. But also very easy and straight forward to use. + +## Example + +Lets take a look at the following sample program. Basically all you have to do is to add a function pointer such as "calculate" to the definition of struct "something_s". Later, during the struct initialization, assign a function address to that function pointer: + +``` +#include + +typedef struct { + double (*calculate)(const double, const double); + char *name; +} something_s; + +double multiplication(const double a, const double b) { + return a * b; +} + +double division(const double a, const double b) { + return a / b; +} + +int main(void) { + something_s mult = (something_s) { + .calculate = multiplication, + .name = "Multiplication" + }; + + something_s div = (something_s) { + .calculate = division, + .name = "Division" + }; + + const double a = 3, b = 2; + + printf("%s(%f, %f) => %f\n", mult.name, a, b, mult.calculate(a,b)); + printf("%s(%f, %f) => %f\n", div.name, a, b, div.calculate(a,b)); +} +``` + +As you can see you can call the function (pointed by the function pointer) the same way as in C++ or Java via: + +``` +printf("%s(%f, %f) => %f\n", mult.name, a, b, mult.calculate(a,b)); +printf("%s(%f, %f) => %f\n", div.name, a, b, div.calculate(a,b)); +``` + +However, that's just syntactic sugar for: + +``` +printf("%s(%f, %f) => %f\n", mult.name, a, b, (*mult.calculate)(a,b)); +printf("%s(%f, %f) => %f\n", div.name, a, b, (*div.calculate)(a,b)); +``` + +Output: + +``` +pbuetow ~/git/blog/source [38268]% gcc methods-in-c.c -o methods-in-c +pbuetow ~/git/blog/source [38269]% ./methods-in-c +Multiplication(3.000000, 2.000000) => 6.000000 +Division(3.000000, 2.000000) => 1.500000 +``` + +Not complicated at all, but nice to know and helps to make the code easier to read! + +## Taking it further + +If you want to get a bit further type "Object-Oriented Programming with ANSI-C" into your favourite internet search engine, you will find some crazy stuff. Some go as far as writing a C preprocessor in AWK, which takes some object oriented pseudo-C and transforms it to plain C so that the C compiler can compile it to machine code. This is actually similar to how the C++ language had its origins. + +E-Mail me your throughts at comments@mx.buetow.org! diff --git a/content/gemtext/gemfeed/atom.xml b/content/gemtext/gemfeed/atom.xml index 8d28fd1d..1403a85e 100644 --- a/content/gemtext/gemfeed/atom.xml +++ b/content/gemtext/gemfeed/atom.xml @@ -1,6 +1,6 @@ - 2021-05-01T13:24:15+01:00 + 2021-05-01T18:36:51+01:00 buetow.org feed Having fun with computers! @@ -28,6 +28,17 @@ comments@mx.buetow.org + + Methods in C + + gemini://buetow.org/gemfeed/2016-11-20-methods-in-c.gmi + 2021-05-01T18:36:51+01:00 + You can do some sort of object oriented programming in the C Programming Language. However, that is very limited. But also very easy and straight forward to use.. .....to read on please visit my site. + + Paul Buetow + comments@mx.buetow.org + + Offsite backup with ZFS (Part 2) -- cgit v1.2.3