C pointers as a linguistic paradox

    Recently, an acquaintance I know through completely non-programmer circles asked to help him with the laboratory in C ++. The code was about the following:

    voiddo_something(MyObj *input[], int count){
        MyObj **copy = new MyObj*[count];
        for (int i = 0; i < count; ++i)
            *copy[i] = *input[i];
        ...
    }

    This case recalled that the topic of pointers is perhaps the main source of errors for students of the language, and at the same time - a kind of pass, overcoming of which is associated with a “click” in the head, which, alas, not all teachers can cause. I hope that the linguistic analogy proposed below will help current students to comprehend this concept, and their teachers or friends will bring this knowledge to them.

    Ten years ago, on one forum , a children's, like a riddle was made :
    Why do I
    eat
    lunch
    ? Cannibal Eater Has invited a Cannibal Eater for lunch ?
    I want to show that this riddle is most directly related to C / C ++, since the topic of pointers can easily be disassembled by analogy.

    See it. In order to understand what “eating cannibal food” is, we need to make a linguistic analysis of the last word. The easiest way to do this is to read the roots from right to left:

    • Ogre = one who eats people.
    • Cannibal = a man who eats cannibals = a man who eats those who eat people.
    • Cannibal = = one who eats cannibals = one who eats those who eat cannibals = one who eats those who eat those who eat people.

    By luck, the same reading order - from right to left - is used in C / C ++ for reference data types:

    • MyClass * = pointer to MyClass.
    • MyClass ** = pointer to MyClass * = pointer to pointer to MyClass.
    • MyClass *** = pointer to MyClass ** = pointer to pointer to MyClass * = pointer to pointer to pointer to MyClass.

    With the right side figured out, now we take for the left.

    • Lunch cannibal = eating lunch who eats cannibal = cannibal.
    • Eating cannibal food = cannibal eating = eating a cannibal eating = cannibal.

    Therefore, the cannibalist (= who knows those who eat cannibals) invited the cannibal to dinner. Obviously, the goal of such an invitation is to feed its subordinate cannibals.

    That is, the linguistic rule turns out to be this: putting the word “food” or “lunch” to the left of the word ending with the root “ed”, we drop this last root, leaving only the vocabulary base before it.

    But pointers in C / C ++ also behave in the same way: if to the left of an entity that has the type “pointer to T” or “T *”, we put the pointer character (that star), then as a result we will throw off one extra pointer from of the type, having received, in fact, T (or, to be completely accurate, T &, but now I intentionally don’t want to touch on the topic of links and other lvalues). Quite roughly (ignoring some of the syntax rules), we can write that

    • * (T *) === T,
    • * (T **) === T *,
    • ** (T **) === T,
    • * (T ***) === T **,
    • and so on.

    What is the result? I did not formulate any clear rules and did not say anything new about pointers. I only hope that this linguistic analogy will help anyone studying C / C ++ to understand this topic more quickly. If you take the asterisk for food, and Lyuda for the basic type ... You understood.

    Also popular now: