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.
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.
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.
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.
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…
Now let’s make something similar to this in C.
Our filter function…
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.
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…
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.
You can store anything on a struct including pointers, arrays, and other structs.
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.
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.
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.
Trying to compile and run this program will cause an error since the compiler reads our c source code line by line…
- gcc is installed in your machine
- gcc is registered to PATH
- familiarity with some basic terminal commands since gcc is a CLI tool
gcc -c filename.c
Running the program:
./a.out (Mac and Linux)
Compiling and Linking in a single command:
For our example, let us first create a simple c program and call it hello.c
Now that we’re done coding, we are now ready to build our program. …