C programming conundrums — Part I

Chaitra Ramaiah
Dev bits
Published in
3 min readMar 1, 2019

--

Challenge yourself with these tiny programming twisters

Photo by Goran Ivos on Unsplash

C Language has survived a long way from its inception and still going strong in an era dominated by its urbane brethren aka the higher level languages. While it seems that the other programming languages have usurped the erstwhile domain niches that were C’s stronghold, this vintage programming language still survives all the vicissitudes that software development chimes in. Many higher level languages and compilers are indeed built on a solid foundation that C provides; they are written in C.

While C seems to loosen its grips in the application realm, where it really shines is the systems domain. Myriads of system programs are still coded in C language.

In this era where the proclivity for the new gen programmers is on the higher level languages, here are a few small C questions. They are not so convoluted but really whets our thinking a wee bit and brings out the affluence that the ancestral programming language holds in its repertoire.

  1. Pointer to pointer to what???

Consider the following declarations.

char a [11][11];
char *a[11];
char (*a)[11];
char **a;

Big deal? Wait!!

Suppose I input 10 strings of 10 bytes, how do you allocate memory and store the strings in each of the declarations types?

Now change the data type to int. How will you now store integers? Do you guys like Keanu Reeves in matrix movie?

Ok, let’s spruce it up a bit. Please also explain how memory is organized with each type of declaration and the stride length while parsing.

2. A teasing data structure

Consider this declaration.

struct data {
int len;
char *buf;
};

Suppose I tell you to write a function where I pass a string and it’s len, can you store the data in the structure?

void store_data (int len, char *str)
{
struct data *ps;
}

So you do two allocations here. One allocation for the pointer ps and other allocation for ps->str (of size len).

Here comes the question. Can you design a data structure where you can store the same data with a single allocation (single malloc)?

3. No polygamy allowed

An easy puzzle. Assume an integer non-sorted array. The array contains duplicate entries (exactly 2) for each value but for one member which is single. Can you find the non-duplicate member by traversing the array only once?

4. A “bit” tedious

Assume an integer pattern like 01110011. You need to toggle bits from 2 position to 5th position. So the pattern becomes 01101101. Can you achieve this with a single C statement?

void toggle_bits(int n, int p, int q) where n is the 8 bit integer, p is the start position and q is the end position. Be on the alert that other bits have to remain the same!!

Please type in your answers in the comments section below. Let’s have a healthy discussion and try the Keep the ‘C’ spirit burning bright. I hope the questions are valid and carry no ambiguity. If you find any ambiguity please correct me.

--

--

Chaitra Ramaiah
Dev bits

Photographer, small-time writer of both prose and poetry, ardent wildlife lover, and a fitness freak. BTW, did I say I am a metalhead too :-)?