I devised two solutions for this month’s Exercise. The basic solution can be solved in two steps, with a more involved solution using three.
Continue reading
Author Archives: dgookin
A Curious Thing About Array Notation
It’s nuts. All through your C training when you learned about arrays, you were taught the format: array[x]
where array
is the name of the array and x
is the element number. But this expression can also be written as x[array]
.
Mind. Blown.
Continue reading
Find the Duplicates
You’ll find multiple reasons to remove duplicates from a series of random numbers. The main reason is that the process or generating random numbers is capable of yielding duplicate values. But more importantly, if your list represents discrete items in the real world, such as playing cards, you can’t really have a duplicate value pop up.
Continue reading
Exit Status Defined Constants
The tradition for successfully exiting a command line program is to use return 0;
or, as I write it, return(0);
. Zero is the OK value, meaning a program exited cleanly. Other values returned represent specific conditions, not necessarily errors, but information that can be communicated to the operating system or some other program.
Continue reading
The Element Doesn’t Exist
As a mid-level language, C has just as many positives as negatives with regards to accessing memory. One of the biggest negatives is C’s unflinching capability to access data that doesn’t exist.
Continue reading
Variable Length Arrays
The C99 standard added a feature to dimension an array on-the-fly. The official term is variable length array or VLA. And while you might think everyone would love it, they don’t.
Continue reading
The Wandering King – Solution
This month’s Exercise was inspired by a program I recall from years ago called The Drunk and the Lamppost. It’s not the classic joke, but an examination of random movements and probability.
Continue reading
Declaring a String Literal as a Pointer
The best way to declare a string literal in your code is to use array notation, like this:
char string[] = "I am some sort of interesting string.\n";
Continue reading
The Wandering King
The chessboard is empty. In the center is the king. He can move only one square at a time, but in any direction — or he can choose not to move at all. How many turns would it take him, moving randomly, to exit the chessboard?
Continue reading
Does the File Exist?
Many programs rely upon external files to store data. When the file is missing, or not where it should be, the fopen() function returns a NULL
pointer, which your code can act upon. Or, you can pre-check for the file’s existence by using the access() function.
Continue reading