diff options
Diffstat (limited to 'content/html/gemfeed')
| -rw-r--r-- | content/html/gemfeed/2016-11-20-methods-in-c.html | 82 | ||||
| -rw-r--r-- | content/html/gemfeed/atom.xml | 13 |
2 files changed, 94 insertions, 1 deletions
diff --git a/content/html/gemfeed/2016-11-20-methods-in-c.html b/content/html/gemfeed/2016-11-20-methods-in-c.html new file mode 100644 index 00000000..5de22d33 --- /dev/null +++ b/content/html/gemfeed/2016-11-20-methods-in-c.html @@ -0,0 +1,82 @@ +<!DOCTYPE html> +<html lang="en"> +<head> +<title>buetow.org - Having fun with computers!</title> +<meta charset='utf-8'> +<link rel="shortcut icon" type="image/gif" href="/favicon.ico" /> +<style> +body { background-color: #282c34; color: #dfdfdf; margin: auto; max-width: 900px; } +img { display: block; max-width: 70%;} +pre { color: #ecbe7b; } +blockquote { color: #ecbe7b; } +a { color: #c678dd; text-decoration: none; } +a.textlink:before { content: " > "; padding-left: 2px; } +a:hover { color: #51afef; text-decoration: underline; } +h1 { color: #ff6c6b; } +h2, h3, h4, h5, h6 { color: #51afef; } +li { color: #98be65; } +</style> +</head> +<body> +<pre>Written by Paul Buetow 2016-11-20</pre> +<a class="textlink" href="../">Go back to the main site</a><br /> +<h1>Methods in C</h1> +<p>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.</p> +<h2>Example</h2> +<p>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:</p> +<pre> +#include <stdio.h> + +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)); +} +</pre> +<p>As you can see you can call the function (pointed by the function pointer) the same way as in C++ or Java via:</p> +<pre> +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)); +</pre> +<p>However, that's just syntactic sugar for:</p> +<pre> +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)); +</pre> +<p>Output:</p> +<pre> +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 +</pre> +<p>Not complicated at all, but nice to know and helps to make the code easier to read!</p> +<h2>Taking it further</h2> +<p>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.</p> +<p>E-Mail me your throughts at comments@mx.buetow.org!</p> +</body> +</html> diff --git a/content/html/gemfeed/atom.xml b/content/html/gemfeed/atom.xml index 37e64837..57922425 100644 --- a/content/html/gemfeed/atom.xml +++ b/content/html/gemfeed/atom.xml @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom"> - <updated>2021-05-01T13:24:15+01:00</updated> + <updated>2021-05-01T18:36:51+01:00</updated> <title>buetow.org feed</title> <subtitle>Having fun with computers!</subtitle> <link href="https://buetow.org/gemfeed/atom.xml" rel="self" /> @@ -29,6 +29,17 @@ </author> </entry> <entry> + <title>Methods in C</title> + <link href="https://buetow.org/gemfeed/2016-11-20-methods-in-c.html" /> + <id>https://buetow.org/gemfeed/2016-11-20-methods-in-c.html</id> + <updated>2021-05-01T18:36:51+01:00</updated> + <summary>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.</summary> + <author> + <name>Paul Buetow</name> + <email>comments@mx.buetow.org</email> + </author> + </entry> + <entry> <title>Offsite backup with ZFS (Part 2)</title> <link href="https://buetow.org/gemfeed/2016-04-16-offsite-backup-with-zfs-part2.html" /> <id>https://buetow.org/gemfeed/2016-04-16-offsite-backup-with-zfs-part2.html</id> |
