Coding C Arrays to hold multiple datatypes simultaneously – Part 1

img_2167Yesterday I pivoted to C with J. I had him start to build out an array for abstract datatypes in C. Initially we were focusing on writing pseudocode with sorting algorithms. Last week J concretely stated that he wants to work for the NSA doing cyber-security. The decision to pivot to writing all whiteboard code in C was instant.

If J is going to work for the NSA he needs to get really really really good at C really really really fast. C is the grandfather of so many programming languages.

I had to decide what problem to have J code. The hardest things for me, in learning C, have been pointers, addresses, and the varying compiler-specific-differences in memory size allocation.

With this in mind, I struggled to come up with a project to illustrate the power and limitations of C.

Enter the almighty array.

I remember taking a poll on Twitter a few years back with my programmer friends. This is going to sound lame to any non-programmers, but the poll question was:

“What’s your favorite data structure?”

Instantly, I responded, “The almighty array!”

All of computer science can be summed up in 1 word: lists. We’re interested in manipulating these lists in C with a data structure called an array. Arrays give us the ability to sort our lists, tell you what is contained in each element of the list, remove something from the beginning of the list, remove something from the end of a list, add something to the beginning, add something to end, append, and prepend. Unfortunately, C is so old, it wasn’t made to hold multiple items of different types in the same list.

“Passing an array of known size by value would require pushing all the array content on the stack. This feature would encourage memory consumption, when memory was sparse, and moving the memory was slow. Passing an array of variable size would have required pushing the size and the content, and calculate dynamically where to find the remaining arguments on the stack. An unacceptable overhead for an OS developer!” – StackExchange

C is an old programming language that was created at AT&T in the mid 1960’s to early 1970s by Dennis Ritchie. This one dude created an computer language then rewrote the Unix operating system. It’s what OSX on Macs is based on.

One of the draw backs of C is that it does not have “first-class” arrays.

This limitation is exactly what I want J to code around. I’ve asked him to come up with ways to do array operations in C. The operations we’re interested in coding up are: pop, push, append, prepend, and length.

He’s started with length in pseudocode. I’m excited to see how this turns out. I’m interested in seeing how he stores various datatypes in the array. We’re going to be working with storing strings, ints, chars, and pointers. I’m excited to see how J tackles this problem set 🙂


Author: David Neely

Professional Software Developer. Technology and Web Coordinator at the University of Hawaii's Manoa Career Center.