pointer-to-pointer to char

A

Andrew Poelstra

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.
 
J

Jack

Andrew said:
No problem. Note: I fixed minor spacing issues above.

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 |
-------------------------------------
 
A

Andrew Poelstra

Andrew said:
No problem. Note: I fixed minor spacing issues above.

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 |
----------------------------------
| | |
| | \--------\
| | |
| \-------------\ |

Absolutely! Congratulations on making an array of pointers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top