On 2006-05-30, Jack <(E-Mail Removed)> wrote:

>

> Andrew Poelstra wrote:

>> On 2006-05-29, Jack <(E-Mail Removed)> wrote:

>> >

>> > Andrew Poelstra wrote:

>> >> On 2006-05-29, Jack <(E-Mail Removed)> wrote:

>> >> > Thanks a lot. I tested your code. It works well!

>> >> > Do you mean that the pointers stored in p1, (p1+1) and (p1+2) must be

>> >> > contiguous in memory?

>> >> > My thought is that p1, (p1+1) and (p1+2) are contiguous in memory. So

>> >> > even if b1, b2 and b3 in my code are not contiguous in memory, I can

>> >> > still access it through p1, (p1+1) and (p1+2). I must be wrong. Why?

>> >> >

>> >> p1+1 points to the memory location immediately after the original p1.

>> >> Here's a small diagram to help:

>> >>

>> >> --------------------

>> >> | p1 | p1+1 | p1+2 |

>> >> --------------------

>> >> | | |

>> >> --------------------------------

>> >> | b1 | ? | ? | b2 | b3 |

>> >> --------------------------------

>> >>

>> >> That is a very artificial example, as b1, b2, and b3 could be

>> >> millions of bytes away from each other! However, p1+1 will always

>> >> point to the place after p1.

>> >

>> > Do you mean that the memory that p1, (p1+1) and (p1+2) point to must be

>> > contiguous?

>> > p1, (p1+1) and (p1+2) are contiguous. b1's address is stored in p1,

>> > b2's address is stored in (p1+1), and b3's address is stored in (p1+2).

>> > Why b1, b2, and b3 must be contiguous? Thanks.

>> >

>> If b1's address is stored in p1, you have double indirection. I'm not

>> sure that that is what you mean or want, but here's an expaination:

>>

>> ---------------------------

>> | p1 | p1+1 | p1+2 |

>> ---------------------------

>> | | |

>> ---------------------------

>> | *p1 | *(p1+1) | *(p1+2) |

>> ---------------------------

>> | | \-----\

>> | | |

>> | \-----------\ |

>> --------------------------------

>> | b1 | ? | ? | b2 | b3 |

>> --------------------------------

>>

>> In this case, *p1, *(p1+1), and *(p1+2) are all pointers in themselves.

>> I recommend you avoid double indirection until you have a firm grasp

>> on other aspects of pointers.

>

> Thank you for your creative figure using text format.

> The figure below expresses what I mean. Why it does not work?

>
No problem. Note: I fixed minor spacing issues above.

> p1 = &b1;

> p1++;

> p1 = &b2;

> p1++;

> p1 = &b3;

>

> p1 = p1-2;

>

> Does p1 point to b1 now? I can not figure out what the problem is.

>

> Thanks a lot.

>
Let's say that &b1 is 1000, &b2 is 1500, and &b3 is 2000.

Here is your code:

p1 = &b1; /* p1 = 1000 */

p1++; /* p1 = 1001 */

p1 = &b2; /* p1 = 1500 */

p1++; /* p1 = 1501 */

p1 = &b3; /* p1 = 2000 */

p1 = p1-2; /* p1 = 1999 */

As you can see, 1999 is not defined in this example, and therefore

when you attempt to dereference it, it is undefined. I'm not sure

what exactly you want the ++'s to do, but that isn't how they work.

--

Andrew Poelstra <

http://www.wpsoftware.net/blog >

To email me, use "apoelstra" at the above address.

You can lead a blind man to water but you can't make him chug it.