1

Here is a predict the output program asked in my recent online test. I was unable to get the accurate output because I am not familiar with double pointers dereferencing. I am aware of the precedence order and the side effects of postfix operation which plays a vital in understanding pointer questions.

Kindly help me with this code snippet by giving examples of double-pointer dereferencing. For more info on single-pointer dereferencing here is a very helpful answer: Pointer expressions: *ptr++, *++ptr and ++*ptr

    #include <stdio.h>
    int main()

    {
        char *a[] = {"NITK", "SURATH", "KALMAN", "GALO", "BREAK", "REJOIN"};
        char **b[] = {a + 2, a + 3, a, a + 1, a + 5, a + 4};
        char ***c = b;
        *c++;
        printf("%s\t", *++*c);
        printf("%s\t", **c + 2);
        printf("%c", (*(**(c + 2) + 2) + 2));
        return 0;
    }

Expected output:

BREAK EAK T

P.s This question might not have a practical use, but it will be helpful to understand the concept of double pointers.

  • 1
    avoid this kind of expressions. They do not have any practical use. Actually if I see something like this doing the code review - I ask to rewrite the whole thing. – 0___________ Sep 19 '20 at 09:25
  • Agree with @P__J__, but this question is from a test. – Rahul Bharadwaj Sep 19 '20 at 09:27
  • 1
    I agree with @P__J__. As I mentioned this question is from a test, I just wanted to clear my concept of double-pointer dereferencing. – codingenthusiast Sep 19 '20 at 09:31
  • regarding: `char ***c = b;` [three star programmer](https://wiki.c2.com/?ThreeStarProgrammer) – user3629249 Sep 19 '20 at 17:17
  • it is easiest to read an expression, like: `*++*c` from right to left. Which results in: variable c is a pointer preincrement that pointer, dereference that pointer. However, must pay attention to the precedence of the operators, so the `++` will be done first then the two dereferences. Suggest reading [C precedence](https://en.cppreference.com/w/c/language/operator_precedence) – user3629249 Sep 19 '20 at 17:20

2 Answers2

1
  1. *c++; it should be c++; as * does not have anny effect here. after this operation it references reference to second value of the double pointer array b (a + 3)

  2. After dereference it points to pointer to pointer to a+3 it is increased (so it references pointer to a + 4. Another dereference gives pointer to a + 4

  3. As previous operations made c to reference pointer to pointer to to a + 4, the **c references a + 4 so the **c + 2 references the third character of a + 4.

  4. c + 2 is a pointer to pointer to a + 1. Then ** references the the a + 1. Adding 2 to it references the third character of a + 1 which is R. dereferencing this pointer gives char 'R'. Add 2 to it and you get 'T' assuming the ASCII char encoding.

0___________
  • 60,014
  • 4
  • 34
  • 74
1

For starters it is a very silly test. Ignore firms that give such a test with obfuscated code. Do not deal with idiots. Usually such tests are given by low-qualified programmers.

Take into account that an array designator used in expressions with rare exceptions is implicitly converted to pointer to its first element.

So this declaration

char ***c = b;

is equivalent to

char ***c = &b[0];

In this expression statement

*c++;

the dereferenced value of the pointer is not used. So this statement should be written like

c++;

After this statement the pointer c points to the second element of the array b.

You could rewrite the statement above like

c = &b[1];

Now let's consider the expression in the printf call

printf("%s\t", *++*c)

Dereferencing the pointer c you get lvalue b[1] that has the value a + 3. Applying the unary operator ++ to this expression you will get a + 4. And at last dereferencing this expression you will get the lvalue a[4] that is outputted.

BREAK

Now consider the expression in the second call of printf.

printf("%s\t", **c + 2);

Dereferencing the pointer c the first time you will get the lvalue b[1]. After the first call of printf it contains the value a + 4. Dereferencing this expression you get the lvalue a[4] that points to the string literal "BREAK". Adding 2 the pointer expression will point to the third character of the string literal. So there will be outputted

EAK

At last let's consider the expression in the third call of printf.

printf("%c", (*(**(c + 2) + 2) + 2));

As c points to b[1] then using the pointer arithmetic c + 2 the expression will point to b[3] Dereferencing the pointer you will get the lvalue b[3] that contains the value a + 1. Dereferencing this expression you will get the lvalue a[1] that points to the string literal "SURATH". Again applying the pointer arithmetic you will get a pointer that points to the third element of the string literal that is to the sub string "RATH". Dereferencing the pointer expression you will get the character 'R'. Addi g to the character the value 2 you will get the character 'T' that is outputted

T

That is all.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335