Jump to content

Pointers

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
In this thread, julmuri posted some code. I was doing some exercises with pointers and what I thought I understood now I don't.

The intresting piece is in bold

for ( std::size_t i = 0; i < size && result; ++i, result = stream.good() )

    stream << [B]*data++[/B] << std::endl;

stream.flush();


Here's a piece of code from my exercise

int b;

int* p1;


b = 2;

p1 = &b;

*p1++;

std::cout << *p1 << std::endl;

I did some research (Operator precedence @ wiki) and as you can see ++ is far above * dereference operator. When I run my code without parenthesis I get some junk result, since I guess, address gets incremented first. But if I use parenthesis (ie (*p1)++; ) it works. The thing I don't understand how his code works without parenthesis and mine doesn't.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#2
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
Postfix ++ and -- operators work like this:
1. Save current value of variable into temporary variable t
2. Increment (decrement) original variable
3. Return temporary variable t.

Prefix ++ and -- operators work like this:
1. Increment (decrement) variable
2. Return variable

Knowing this, *p++ is executed like this:
1. Take value of p into temporary variable p1
2. Increment value of p
3. Dereference temporary variable p1.

The first piece of code works well because data is an array of values. Hence, *data++ prints current value in the array and moves to next one (which will be printed in next iteration).

Your piece of code doesn't work because you've incremented p1 in *p1++ statement. So once you try to print *p1, it already points to next location in memory which is junk.

#3
artificial

artificial

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 624 posts

Flying Dutchman said:

The thing I don't understand how his code works without parenthesis and mine doesn't.

There are AFAIK two reasons for that:
1) He uses a non-standardized compiler.
2) data is an array of pointers, so that he fetches the next element's pointer and dereferences its value.

EDIT: I should've hurried up. ^^

Greets,
artificial

Edited by artificial, 14 August 2010 - 06:28 AM.
EDIT

Sometimes words ain't enough to express something. That's why computer scientists use double words.

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1

"artificial" said:

He uses a non-standardized compiler.
I compiled both his and mine code on my pc. zoranh, thanks for explanation. :)
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users