0
#include<stdio.h>
void main()
{
 char ***p="hello";
 printf("%c",++*p++);
}

I haven't understand why the (*) indirection operator used here three times.

When i compiled this program then the output was "j". But actually hear the p is a pointer to pointer to pointer to Array of character. Then why i getting the output as j. I didn't understand what's the logic behind this. Please help me to understand the actual logic behind this.

And the confusion increase more when I only use one indirection operator and get complied the program .then the output is i.means

void main()
{
 char *p="hello";
 printf("%c",++*p++);
}
kTiwari
  • 1,488
  • 1
  • 14
  • 21
  • Just want to let you know that `void main` isn't C: http://stroustrup.com/bs_faq2.html#void-main – chris Jul 23 '12 at 16:49
  • 3
    It is undefined behavior. [Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc...)](http://stackoverflow.com/questions/949433/could-anyone-explain-these-undefined-behaviors-i-i-i-i-i-etc) – pmr Jul 23 '12 at 16:50
  • 2
    It looks like it's relying on all of the undefined behavior ever. – Wug Jul 23 '12 at 16:50
  • 4
    @Wug, psh, it's missing a whole slew of `setjmp`/`longjmp` undefined behaviours! – Blindy Jul 23 '12 at 16:53
  • @chris: thanks you chris for giving me a new knowledge but I couldn't get any stuff from that line that would have supported your statement.can you please explain me ? – kTiwari Jul 23 '12 at 16:56
  • @krishnaChandra, From the creator of C++: *The definition void main() { /* ... */ } is not and never has been C++, nor has it even been C.* `main` should return an integer to the OS. – chris Jul 23 '12 at 17:05
  • @chris: thank you chris ,now I got it – kTiwari Jul 23 '12 at 17:24
  • There's no "indirection operator used 3 times" in your code. The `*` in the declaration of `p` is not "indirection operator". It is not an operator at all. It is just a `*` - a part of the pointer declaration syntax. – AnT stands with Russia Jul 23 '12 at 18:52
  • @pmr: The OP's code does not suffer from undefined behavior described at your link. If the pointer values involved are valid, the `++*p++` expression is perfectly legal and has perfectly defined behavior. There are no `i = i++ + ++i`-style issues here. – AnT stands with Russia Feb 14 '14 at 00:52

3 Answers3

2

The code has no meaningful output as C code. The code is simply invalid.

Firstly, void main is not a valid declaration for main function in C. main must be declared as returning int.

Secondly, the char ***p="hello" initailization is invalid. String literal decays to has type char * in C. A value of type char * cannot be used to initialize an object of type char ***.

Thirdly, dereferencing a char *** pointer produces a pointer value of type char **, which cannot be printf-ed with %c format specifier.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
1

First, it is an undefined behavior.

Second, it isn't a correct C (or C++) code either. It generates this warning due to non-const char pointer.

3.c: In function 'main':
3.c:5:12: warning: initialization from incompatible pointer type [enabled by default]
  char ***p="hello";
            ^

Better do it this way:

#include <stdio.h>

int main()
{
    const char ***p="hello";
    printf("%c",++*p++);

    return 0;
}

And regarding what it prints: the program has requested the run-time to terminate it in an unusual way. But this is an undefined behavior - your results may vary.

Sergey K.
  • 24,894
  • 13
  • 106
  • 174
1

The reason you don't understand why multiple indirection was used is because multiple indirection doesn't make sense here; it's just one of the many problems in this code.

"hello" is string literal, which is also an array expression, specifically "6-element array of char", which in this context is converted ("decays") to a pointer to char; p should be declared as

char *p = "hello";

The expression ++*p++ attempts to increment the thing p points to (in this case, the character h), and as a side effect advances p to point to the next character. The behavior of modifying a string literal is undefined; depending on the platform, string literals may not be writable, and the expression ++*p++ may lead to an access violation.

The following is a corrected version of the above program, which gives the intended result:

#include <stdio.h>

int main(void)
{
  char h[] = "hello";
  char *p = h;
  printf("%c\n", ++*p++);
  return 0;
}

Instead of having p point to a string literal, we have it point to a local buffer that's been initialized with the contents of the string literal.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • sir could I know the term side effect you used hare. – kTiwari Jul 23 '12 at 18:48
  • @krishnaChandra: "side effect" simply means that something gets modified as a result of evaluating the expression. For example, assume `y = 0` and `x = 1`. The expression `y=x++` has two side effects -- both `y` and `x` are modified as a result of evaluating the expression. `y` gets the value 1 (value of `x` before the increment), and `x` gets the value 2 (new value after the increment). The expression `++*p++` also has two side effects; both the pointer `p` and the thing it points to are being incremented by 1. – John Bode Jul 23 '12 at 19:19
  • sir Could you please provide me some link that will help me understand side effect in detail. – kTiwari Jul 23 '12 at 19:22
  • @krishnaChandra: [Side Effect (computer science)](http://en.wikipedia.org/wiki/Side_effect_(computer_science)), from Wikipedia. Also see the [C 2011 standard](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf), section 5.1.2.3 (Program Execution), paragraph 2. – John Bode Jul 23 '12 at 19:30