Click here to Skip to main content
15,882,055 members
Please Sign up or sign in to vote.
3.22/5 (3 votes)
See more:
#include <iostream>

using namespace std;

int main() {
int a[] = { 5,7,9,11,14};
int *p= a+2;
cout<<++*p++;
}

What I have tried:

I think the output should be 8 but it is 10.Why its giving 10 as output?
Posted
Updated 28-Dec-17 6:48am
Comments
[no name] 28-Dec-17 9:21am    
Interesting. Please explain why you expect 8. At the Moment I don't see why it should be 8 nor 10.
See e.g. this:C++ Pointer Arithmetic[^]
Richard MacCutchan 28-Dec-17 9:43am    
Because it is the sort of code that only confuses. Get a proper book on C and learn the basics.
ZurdoDev 28-Dec-17 9:48am    
Fair point, but I believe this exercise could be useful in understanding certain concepts of pointers and order of precedence, etc.
[no name] 28-Dec-17 9:51am    
No not "Fair Point". I think we all had to learn while diving into strange things. Understand the "strange" things will help to learn in depth.

[Edit] and calling the OP a child/idiot is far away to be fair!
Richard MacCutchan 28-Dec-17 11:18am    
No, it is more likely to confuse people. The trouble is that more and more questions are being posted like this, by people who do not understand the basics of the language. But they still think that starting with overcomplicated questions is a good idea.

Simple: you are doing silly things!
See here: Why does x = ++x + x++ give me the wrong answer?[^]
 
Share this answer
 
Comments
[no name] 28-Dec-17 10:03am    
Can you please explain this more with the given example from OP? At the Moment I really don't see why 8 or 10 should be a result, while checking this for 2/4 Byte integers. Any hint is welcome.
Dave Kreskowiak 28-Dec-17 10:17am    
What's to explain? The implementation of the prefix and postfix operators was never clearly defined in the original C specification and, because of this, are entirely dependent on the opinions of the people who wrote their C compilers. The exact same code compiled by two different C compilers can result in differing outputs.

The code the OP posted has no defined correct answer because the code is using operators that generate differing machine code dependent on the compiler used.
[no name] 28-Dec-17 10:22am    
"What's to explain?"
Postfix is out of question here (in my opinion), please try to be constructive ;)
Dave Kreskowiak 28-Dec-17 10:31am    
I was being constructive. The problem is that you didn't read the article Griff linked to.
Member 10585568 28-Dec-17 11:45am    
My confusion is not with prefix or postfix.

I was confused with the value of a in the operation *p=a+2.I think,a should be 5(0th element of array),but i got 10 as output so in that way a is 7(1st element of the array).I just want to clear this point.
Add a couple of cout lines to the code and then run the code under the debugger, after putting a breakpoint on the first line under int main(). Step through the code line-by-line in the debugger, examining the contents of the variables before and after each line of code is executed and it should become obvious what's going on.
C
<pre>int main()
{
    int a[] = { 5, 7, 9, 11, 14 };

    int *p = a + 2;

    cout << *p << endl;

    cout << ++*p++ << endl;

    cout << *p << endl;
}

The debugger is a tool to debug YOU and your understanding of the code, not the code itself. You have to learn to use this tool or you're just going to be guessing at what you're code is really doing.

How a compiler and CPU interpret and execute the code can be vastly different from how you interpret and execute the code in your head.
 
Share this answer
 
v3
Comments
[no name] 28-Dec-17 10:35am    
That is constructive! Still postfix is out of question ;)
And still don't see why 10 Comes out, most probably I Need to check MSB vs LSB?
Dave Kreskowiak 28-Dec-17 10:40am    
No, it's not. The pre and postfix operators are at the very heart of this problem. They are what's so confusing to people who look at code like this and can't understand what's going on.

The underlying problem isn't what the correct answer is. The problem is understanding the operators, how they do their jobs, and more importantly, WHEN they do their jobs.

You're looking too deep into this. It has nothing to do with MSB over LSB. You have to do exactly what I described in my answer.
[no name] 28-Dec-17 10:42am    
But in case I don't apply post fix I get 10 *confused*
Dave Kreskowiak 28-Dec-17 10:44am    
Like I said, the debugger is there to debug YOU, not the code.
[no name] 28-Dec-17 10:46am    
I'm on Debugging. Maybe better to do it tomorrow again.... hmmm
[Edit]
Damed, I don't get the Point where I'm wrong, please help, I see always 10 and can't explain!


[Edit]
{ int a[] = { 5, 7, 9, 11, 14 };

int *p = a + 2;

int xP= *p;
if (xP) // xP shows me what I expect, "9"
;

int xPP= ++*p;
if (xPP) // xPP Shows me 10, No idea why, please explain
;
}
Putting all else aside, which is not easy
C++
int *p=a+2; // Increments two sizeof(int) - that is, two elements NOT two bytes
So, since a is the address of a[0] then a+2 is the address of a[2]
That means it's pointing to 9.
If you increment 9 it give you 10.

Make sense, now? Good. Now go study point arithmetic and usage in C/C++. It's incredibly useful, powerful, and without mastering it you're wasting your time.
 
Share this answer
 
Comments
[no name] 28-Dec-17 12:24pm    
Oh wtf, how I could missed this, pointer vs. value! Thank you very much! Sometimes (a lot of times) explain it in other words does solve the conflict :) Makes sense now.
Dave Kreskowiak 28-Dec-17 13:08pm    
Hahaha... still wrong. a is a pointer! It's holding the address of the first value in the array. When you add 2 to it, you're getting the address incremented by (sizeof(int) * 2). The compiler is hiding these little details from you to make the code easier to read but harder to understand what's going on behind the screen.

Oh, and you never said this is what you were having a problem with. You were stuck on the pre and postfix operators, which you apparently never read the article on, WHICH GOES INTO THE DETAIL YOU'RE DEMANDING HERE!
[no name] 28-Dec-17 13:16pm    
haha no, no: "You were stuck on the pre and postfix operators". Post fix at least are out of question here. If you still think there are, you have a Problem with them ;)
I'm not the first who is mentioning this: Please respond in a appropriate way.
PLEASE, YES?
Dave Kreskowiak 28-Dec-17 13:18pm    
Hahaha. Have a nice life.
Member 10585568 28-Dec-17 13:10pm    
Thanks a lot .......sir.
Do not combine things in same line if you don't understand what you combine.
Change your code to:
C++
#include <iostream>
using namespace std;

int main() {
  int a[] = { 5,7,9,11,14};
  cout<<"A:" << *a << endl;
  int *p= a+2; // this is pointer arithmetic 
  cout<<"B:" << *p << endl;
  // the result comes from
  cout<<"C:" << ++(*p) << endl;
  cout<<++*p++;
}
 
Share this answer
 
Comments
[no name] 28-Dec-17 12:58pm    
Sorry, my 3 but this is now superfluous and does not explain the what and why and whatelse
Patrice T 28-Dec-17 13:01pm    
No problem, 3 is neutral.
My solution is in code to execute.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900