Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pointer-to-pointer to char

Reply
Thread Tools

pointer-to-pointer to char

 
 
Andrew Poelstra
Guest
Posts: n/a
 
      05-30-2006
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.
 
Reply With Quote
 
 
 
 
Jack
Guest
Posts: n/a
 
      05-30-2006

Andrew Poelstra wrote:
> 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.
>


Thanks a lot. I understand now.

If I modify my code as below:

int *p1[3];
p1[0] = &b1;
p1[1] = &b2;
p1[2] = &b3;

Now the following figure should work, right?
------------------------------------
| p1 | p1+1 | p1+2 |
----------------------------------
| | |
| | \--------\
| | |
| \-------------\ |
-------------------------------------
| b1 | ? | ? | b2 | b3 |
-------------------------------------

 
Reply With Quote
 
 
 
 
Andrew Poelstra
Guest
Posts: n/a
 
      05-30-2006
On 2006-05-30, Jack <(E-Mail Removed)> wrote:
> Andrew Poelstra wrote:
>> On 2006-05-30, Jack <(E-Mail Removed)> wrote:
>> > 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.
>>

>
> Thanks a lot. I understand now.
>
> If I modify my code as below:
>
> int *p1[3];
> p1[0] = &b1;
> p1[1] = &b2;
> p1[2] = &b3;
>
> Now the following figure should work, right?
> ------------------------------------
> | p1 | p1+1 | p1+2 |
> ----------------------------------
> | | |
> | | \--------\
> | | |
> | \-------------\ |
> -------------------------------------
> | b1 | ? | ? | b2 | b3 |
> -------------------------------------
>


Absolutely! Congratulations on making an array of pointers.

--
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
char *fred; char * fred; char *fred; any difference? Ben Pfaff C Programming 5 01-17-2004 07:37 PM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments