5 Replies - 430 Views - Last Post: 14 January 2012 - 02:16 PM Rate Topic: -----

#1 suthars  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-March 10

purpose of the following lines.

Posted 13 January 2012 - 05:04 PM

Can somebody please explain the purpose of the following lines.

int x[5];
*(x+1)++;
Is This A Good Question/Topic? 0
  • +

Replies To: purpose of the following lines.

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: purpose of the following lines.

Posted 13 January 2012 - 05:16 PM

Is this a test?
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: purpose of the following lines.

Posted 13 January 2012 - 05:16 PM

Context would be important here.

You declare an array of 5 integers and then use pointer notation to access elements in a terrible terrible way. Dereference x+1 and then increment x. No array element has any meaningful data in it. Ugh.
Was This Post Helpful? 1
  • +
  • -

#4 suthars  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-March 10

Re: purpose of the following lines.

Posted 14 January 2012 - 03:08 AM

View Postr.stiltskin, on 14 January 2012 - 12:16 AM, said:

Is this a test?

Yes. This question is from one of the past exam paper.

View PostKYA, on 14 January 2012 - 12:16 AM, said:

Context would be important here.

You declare an array of 5 integers and then use pointer notation to access elements in a terrible terrible way. Dereference x+1 and then increment x. No array element has any meaningful data in it. Ugh.

Thank you. I've got it.
Was This Post Helpful? 0
  • +
  • -

#5 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: purpose of the following lines.

Posted 14 January 2012 - 12:19 PM

*(x+1)++;



The suffix ++ operator takes precedence over the *. So this will be evaluated as:

(*((x+1)++));



x+1 returns temporary object (r-value) and that is not legal to increment. So compilation should fail, so should you if you ever write this code :).

This post has been edited by Karel-Lodewijk: 14 January 2012 - 04:50 PM

Was This Post Helpful? 2
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: purpose of the following lines.

Posted 14 January 2012 - 02:16 PM

View PostKarel-Lodewijk, on 14 January 2012 - 02:19 PM, said:

x+1 returns temporary object (r-value) and that is not legal to increment, so compilation should fail, so should you if you ever write this code :).

LOL. I tried for 15 minutes this morning to find a way to say this that wouldn't confuse the OP even further & eventually just gave up.

So yes, (x+1)++ will always be rejected by the compiler regardless of whether x is an array, or a pointer, or an int, or whatever.

But (assuming x is a pointer or an array) *(x+1) is a value in memory -- an l-value -- so it would be legal to write
(*(x+1))++;

because the outer parentheses force the memory location x+1 to be dereferenced before the increment operator is applied.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1