Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.
#include<stdio.h>
void main()
{
char ***p="hello";
printf("%c",++*p++);
}

I haven't understand the concept of (*) indirection uses 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 and get complied the program .then the output is i.means

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

this gives output as i.

What is the difference between these two program?

share|improve this question
4  
This is horrible code, written to prove a particular point about pointers and indirection, and generally not helping you to understand them. Set it aside and don't try to understand it yet. Understand clean and readable code first, and when triple indirection is starting to make sense to you, and you understand the order of operations for the increment and dereference operators being used, then come back and look at it again. I assure you what you feel will not be "aha! how beautiful!" but more "what idiot thought that was a good example?" –  Kate Gregory Jul 21 '12 at 16:00
add comment

2 Answers

Undefined.

You are printing part of some piece of memory you haven't inicialized yet. It can crash, it can do nothing, it can print garbage.

share|improve this answer
    
@Euphhoric: sir but I compiled these programs and the output was the same what I specified there in my question. –  Krishna Chandra Tiwari Jul 21 '12 at 14:50
6  
Did you compile it with different compilers? Different settings? Different OS? The key idea behind "undefined" is, that you can never tell if the output will remain same no matter what compiler or platform you run it on. –  Euphoric Jul 21 '12 at 14:54
    
I compiled it only one compiler and OS –  Krishna Chandra Tiwari Jul 21 '12 at 16:13
add comment

Firstly you have to understand the ++ operators are undefined when used within a statement - ie they may run before or after the value is used within the printf statement. In fact, I think what it happening in your 2nd example is that you are getting the 'h' from hello, then one of the ++ operators is being applied (making it a 'j') but the other one is being applied after the printf statement has completed. The moral: do not try to be clever with ++ operators.

Now the other part, the 3-level indirection.. WTF?!

1 level of indirection is ok. a "char*" is a pointer to a memory location that contains a character. Easy. This is best thought of as a string type in itself, even though you are storing the string in memory somewhere, and only holding the pointer to that memory as a variable. So your p variable is only 4 bytes long (the length of a pointer), not the length of the string.

When you add another layer of indirection, its nearly always because you want to change the location of the string your variable is pointing to, and the variable has been passed into a function. As all function arguments are passed by-value, you cannot change the pointer, so you pass in a pointer to your pointer which allows you to change the 2nd one.

So you have 'hello' stored in memory somewhere, you pass in a pointer to the pointer to that memory to a function. That allows you to dereference the input to get the pointer to 'hello' and change it.

so

void fn(char** p) { *p = "world"; }
char* p1 = "hello";
fn(&p1);

will work, the location of the p1 variable is passed in, this is a by-value copy, but that's ok because you can derefence it to get the "real pointer" you want to change - p1.

3 levels of indirection... nobody does that, not ever. I think you get a 'j' just by luck, the pointer to a pointer to a pointer to a character is garbage.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.