From ee75979b5d94ae18f930ff91e5b2d51cd554b60d Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Mon, 9 Mar 2026 22:45:54 +0200 Subject: Update content for html --- ...20-object-oriented-programming-with-ansi-c.html | 81 ++++++++++------------ 1 file changed, 38 insertions(+), 43 deletions(-) (limited to 'gemfeed/2016-11-20-object-oriented-programming-with-ansi-c.html') diff --git a/gemfeed/2016-11-20-object-oriented-programming-with-ansi-c.html b/gemfeed/2016-11-20-object-oriented-programming-with-ansi-c.html index 70e1c34e..bedf6692 100644 --- a/gemfeed/2016-11-20-object-oriented-programming-with-ansi-c.html +++ b/gemfeed/2016-11-20-object-oriented-programming-with-ansi-c.html @@ -2,17 +2,12 @@ - Object oriented programming with ANSI C -
-
-
-

Home | Markdown | Gemini

@@ -39,37 +34,37 @@ by Lorenzo Bettini http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite --> -
#include <stdio.h>
+
#include <stdio.h>
 
-typedef struct {
-    double (*calculate)(const double, const double);
-    char *name;
-} something_s;
+typedef struct {
+    double (*calculate)(const double, const double);
+    char *name;
+} something_s;
 
-double multiplication(const double a, const double b) {
-    return a * b;
-}
+double multiplication(const double a, const double b) {
+    return a * b;
+}
 
-double division(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"
-    };
+int main(void) {
+    something_s mult = (something_s) {
+        .calculate = multiplication,
+        .name = "Multiplication"
+    };
 
-    something_s div = (something_s) {
-        .calculate = division,
-        .name = "Division"
-    };
+    something_s div = (something_s) {
+        .calculate = division,
+        .name = "Division"
+    };
 
-    const double a = 3, b = 2;
+    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));
-}
+    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) with the same syntax as in C++ or Java:
@@ -78,8 +73,8 @@ http://www.gnu.org/software/src-highlite --> by Lorenzo Bettini http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite --> -
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));
+
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:
@@ -88,8 +83,8 @@ http://www.gnu.org/software/src-highlite --> by Lorenzo Bettini http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite --> -
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));
+
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:
@@ -98,10 +93,10 @@ http://www.gnu.org/software/src-highlite --> by Lorenzo Bettini http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite --> -
pbuetow ~/git/blog/source [38268]% gcc oop-c-example.c -o oop-c-example
-pbuetow ~/git/blog/source [38269]% ./oop-c-example
-Multiplication(3.000000, 2.000000) => 6.000000
-Division(3.000000, 2.000000) => 1.500000
+
pbuetow ~/git/blog/source [38268]% gcc oop-c-example.c -o oop-c-example
+pbuetow ~/git/blog/source [38269]% ./oop-c-example
+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!
@@ -114,7 +109,7 @@ http://www.gnu.org/software/src-highlite --> by Lorenzo Bettini http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite --> -
mult.calculate(mult,a,b));
+
mult.calculate(mult,a,b));
 

Real object oriented programming with C


@@ -135,11 +130,11 @@ http://www.gnu.org/software/src-highlite -->
Back to the main site
-- cgit v1.2.3