Pointer Arithmetic
One of the strengths of pointers is that you can do basic arithmetic with it. Though arithmetic may originally mean mathematical operators; for pointers, only addition (and subtraction) are defined. Continue reading
One of the strengths of pointers is that you can do basic arithmetic with it. Though arithmetic may originally mean mathematical operators; for pointers, only addition (and subtraction) are defined. Continue reading
New C/C++ programmers often have this confusion that all arrays are pointers. Well… they’re kinda, sorta, but not really. Let’s consider the following code:
1 2 3 4 |
char arr[5] = {'H','e','l','l','o'}; char *p = &arr[0]; printf("%c\n", p[2]); // l printf("%c\n", *arr); // H |
Try compiling the code snippets using Coding Ground.
The pointer p points to the first character of arr. Remember that the index operator ([]) is automatically converted to addition then dereference.
1 |
p[2]; // same as *(p + 2) |
Note: 2[p] would be evaluated to *(2 + p) giving us the same result as p[2]. It’s bad practice to use that form even if it works.
And dereferencing arr is similar to getting the first element
1 |
*arr; // same as *(arr + 0) which is arr[0] |
So when do arrays and pointers differ?
My first semester in DigiPen has just ended and I get to make tutorials again! One of my classes was Game Engine Fundamentals and I used SDL2 to create my game engine. SDL2 is a cross-platform C library to create games on. My favorite tutorial to learn SDL is Lazy Foo. He gives a detailed tutorial in multiple aspects of the library. However, setting up on Mac is not yet done. Hence, I have created this post.
As a continuation to the discussion on Purpose of Pointers to Functions, let’s step up the complexity and have an array of function pointers. This time, we’ll use C++ because we’ll need the inheritance for demonstration 🙂
What’s the purpose of having an array of function pointers?
In a previous post, I mentioned something about pointers to functions. Why would anyone need pointers to functions? In C/C++, they’re called function pointers. They also exist in other languages but the implementations are slightly different. In C#, they’re called delegates. Many OOP languages have interfaces with overrideable methods. In others, there’s the concept of lambda (anonymous) functions.
One common application of function pointers are Event Handlers. Event handlers are quite straight forward. Assign a function as an event handler and when that event is triggered, execute the corresponding function. See W3Schools for more info on JavaScript Event Handling. These functions are referred to as Callbacks.
In C#, they allow adding/subtracting of delegates. Adding a delegate simply adds that method to the list of methods that will be executed, one by one in the order they were added, when the event is triggered. Subtracting removes that method from the list. This allows having multiple listeners to a single event. See MSDN for more information.
What’s the point of pointers to functions?
Using C/C++, what is the fastest way to determine if an integer is a power of 2?
1 2 3 4 5 6 7 |
int x = /* some value */; if (/* is x a power of 2 ?*/) { /* x is a power of 2 */ } else { /* x is not a power of 2 */ } |
Given the statement below, what is pf?
1 |
int (*(*pf)[10][5])(float, double); |
Last school year, I can confidently say that many of my students enjoyed this one lab exercise we did in class. The exercise was to create circular designs using lines with C++. It may sound weird to find students enjoying a combination of programming and trigonometry to create digital math art but it did happen. When I uploaded their submissions in Facebook, I got a couple of private messages from former students complaining why they didn’t have those. And there were a few who even got curious enough to ask me to teach them. Unfortunately, the exercise was a cumulative effort from different activities and I couldn’t teach it in just one sitting. Since then, I put “Mandala Design by Programming” as one of my to-do video lectures.
And here it is…
The CPNG header and source files can be downloaded here. The PNG encoder could be acquired here. Make sure to get the lodepng.cpp and lodepng.h.
In the video, I used XCode to compile. In case you’re compiling by terminal, include the following flags: -ansi -pedantic -Wall -Wextra -O3. For example, if your source code is main.cpp and you wish the executable to be called main.exe, you can compile by
1 |
g++ cpng.cpp lodepng.cpp main.cpp -ansi -pedantic -Wall -Wextra -O3 -o main.exe |
Ideally, this video is for fellow programming teachers to inculcate the value of loops. It can also be a side exercise where students would manipulate lines and colors instead of the usual text or number processing. I think, this can also be valuable for Math teachers teaching trigonometry and help the students appreciate sine and cosine.
If you encountered issues while creating your own Mandala Design, e-mail me at francis dot serina at gmail dot com. Also, I’d like to see your own mandala designs so please post in the comments below the results of your own artwork 😀