Sketch of a commonly used example of polymorphism
Sketch of a commonly used example of polymorphism

Polymorphism is an important part of many design patterns. In most languages that supports OOP this is commonly done through the use of an interface which will then be inherited and implemented by the deriving classes.

In this post we will explore how to do something like that in C.

An Example

A common example to illustrate polymorphism is to have a parent type Animal to be derived by a type Dog and a type Cat with different implementation of the parent type’s methods.

This post is no different because that is exactly what we are going to build.

Struct inheritance?

One problem is…


In this post we will implement a dynamic array similar to the ones seen in other programming languages like the std::vector in C++ and ArrayList in Java.

The Header

Our header file will contain the structure for our dynamic array and a function to initialize it. Let’s also define the initial capacity here.

vector.h

The first member of our struct is the array where we will put all the data. To make its data type generic, we use a void pointer. You can think of the void** as an array that can hold any data type.

The second and third member is…


If you have done some web development and worked with javascript, chances are you have come across a very useful function for manipulating array data called filter. Given an array, filter will apply a given function to each item and then create a new array containing all the items for which the given function returns true.

Here’s an example in javascript:

Now let’s make something similar to this in C.

Let’s do it in C

Our function will look slightly different from the one in javascript.

To make our implementation simple, our filter function will only accept an array of integers.

Our filter function…


What is a function pointer?

A function pointer is like any other pointer but instead of pointing to a variable’s address, it points to the address of a function.

Declaration, Assignment, and Function Call

The program below shows the syntax of how to declare, assign and do a function call using a function pointer.

At line 15 we declared a function pointer called func with the same signature as our function at line 6 which is named ‘add’. We then assigned the address of the function ‘add’. Normally when we are assigning an address of a variable to a pointer, we are adding an ‘&’ in front of the…


What is a Structure?

In C programming, a structure or struct is a user defined data type that can be used to logically group different data types into a single variable.

What can be stored in a struct?

You can store anything on a struct including pointers, arrays, and other structs.

How to create a struct?

We can create a struct by using the struct keyword. Below is an example of a struct called Pokemon which has a char* and int member for storing name and level.

How to declare a struct?

We can declare a struct just like any other basic types as shown in the example below.

You may observe in line 11, the only difference in…


In this post I’ll be talking about function prototypes, header files, and how to break c programs to separate files.

Let’s take a look at the code below.

Example code

At line 3 we wrote the definition of the function named greet() and called it in the main function. Now, what do you think will happen if we move the definition of greet() below the main function? Let’s try and see.

Definition of greet() function moved below main()

Trying to compile and run this program will cause an error since the compiler reads our c source code line by line…


Prerequisites:
- gcc is installed in your machine
- gcc is registered to PATH
- familiarity with some basic terminal commands since gcc is a CLI tool

TL;DR
Compiling:

 gcc -c filename.c

Linking:

gcc filename.o

Running the program:

 ./a.out (Mac and Linux)
a.exe (Windows)

Compiling and Linking in a single command:

 gcc filename.c

Hello World!
For our example, let us first create a simple c program and call it hello.c

hello.c

Now that we’re done coding, we are now ready to build our program. …

Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store