Pointers to Pointers to Char in C
Author Message
Pointers to Pointers to Char in C

Hi everyone...

pointers.

In the following code I'm confused about what d points to. Here's my
(obviously wrong) thinking...

b is a pointer to char which points to the 4th char of array a[]... ('t');

c is a pointer to a pointer to char which is assigned the address
of b...so it holds the address of b. So c points to b.

Then we come to my problem:  *d = *c. It seems to say that d (which
is a pointer to char) is assigned the dereferenced value of c. In
other words, it would seem that d and c both point to b.

But running the program prints "Abbey" which means that d points to
't' and d+5 is the string "Abbey" which means that d doesn't point to
b but points to the SAME address that b does.

HELP!! Thanks...Stephen.  ;>)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main()
{
char a[] = "Tintern Abbey";
char *b=a+3;
char **c=&b;
char *d=*c;
puts(d +5);

Quote:
}

--

*
*-|-*      Stephen Levinson

*-|-*
*

--

Fri, 09 Nov 2001 03:00:00 GMT
Pointers to Pointers to Char in C

Quote:

>     In the following code I'm confused about what d points to. Here's my
> (obviously wrong) thinking...

> b is a pointer to char which points to the 4th char of array a[]... ('t');

> c is a pointer to a pointer to char which is assigned the address
>  of b...so it holds the address of b. So c points to b.

> Then we come to my problem:  *d = *c. It seems to say that d (which
> is a pointer to char) is assigned the dereferenced value of c. In
> other words, it would seem that d and c both point to b.

Here's the problem; since c points to b, *c points to what b points to;
the declaration/initializer syntax should be thought of as char *d
and then d = *c.  (I misread this in a post a week or so ago when
there were two variables declared in the same line and I read the
comma as a semicolon.)  Notice that d has the wrong type to point
to b; it's char *, and c has type char **.

Quote:
> But running the program prints "Abbey" which means that d points to
> 't' and d+5 is the string "Abbey" which means that d doesn't point to
> b but points to the SAME address that b does.

Exactly; this is the result of d = *c.

Quote:
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>

> main()
> {
>  char a[] = "Tintern Abbey";
>  char *b=a+3;
>  char **c=&b;
>  char *d=*c;

This acts like char *d; d = *c;

Quote:
>  puts(d +5);
> }

--
MJSR

--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---
--

Fri, 09 Nov 2001 03:00:00 GMT
Pointers to Pointers to Char in C

Quote:

> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>

> main()
> {
>  char a[] = "Tintern Abbey";
>  char *b=a+3;
>  char **c=&b;
>  char *d=*c;
>  puts(d +5);
> }

Ah, this is what happens when people get too cute with initializers.
Here is an (almost) equivalent version:

int main(void) {

char a[] = "Tintern Abbey";
char *b, **c, *d;

b = a + 3;      /* 'b' points to the letter 't' */
c = &b;             /* 'c' points at the variable 'b' */
d = *c;         /* 'd' now has the value of the pointer 'c'
points at, ie., what 'b' points at */
puts( d + 5 )   /* or a + 8 */

Quote:
}

Regards,

Anton

--

Sat, 10 Nov 2001 03:00:00 GMT
Pointers to Pointers to Char in C

Quote:

> Hi everyone...

> pointers.

>     In the following code I'm confused about what d points to. Here's my
> (obviously wrong) thinking...

Be careful here.  All these initialization of b, c, d are to non-constants.
This is a dangerous road to start down.

Let's look at a modified form of your code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
char a[] = "Tintern Abbey";
char *b = a + 3;
char **c = &b;
char *d = *c;
printf("a is %p; the string is %s\n", (void *)a, a);
printf("b is %p; the string is %s\n", (void *)b, b);
printf("c is %p; *c is %p; the string is %s\n",
(void *)c, (void *)*c, *c);
printf("d is %p; the string is %s\n", (void *)d, d);
printf("d+5 is %p; the string is %s\n", (void *)(d+5), d+5);
puts(d + 5);
return 0;

Quote:
}

The values of the pointers will obviously be different for you, but they are
useful for explication.  I get the following.

a is 2e0fd0; the string is Tintern Abbey
b is 2e0fd3; the string is tern Abbey
c is 2e0fcc; *c is 2e0fd3; the string is tern Abbey
d is 2e0fd3; the string is tern Abbey
d+5 is 2e0fd8; the string is Abbey
Abbey

Quote:

> b is a pointer to char which points to the 4th char of array a[]... ('t');

==

OK:
a is 2e0fd0; the string is Tintern Abbey
b is 2e0fd3; the string is tern Abbey

==

Quote:

> c is a pointer to a pointer to char which is assigned the address
>  of b...so it holds the address of b. So c points to b.

==

OK:
b is 2e0fd3; the string is tern Abbey
c is 2e0fcc; *c is 2e0fd3; the string is tern Abbey

==

Quote:

> Then we come to my problem:  *d = *c. It seems to say that d (which
> is a pointer to char) is assigned the dereferenced value of c. In
> other words, it would seem that d and c both point to b.

==

No ...
b is 2e0fd3; the string is tern Abbey
c is 2e0fcc; *c is 2e0fd3; the string is tern Abbey
d is 2e0fd3; the string is tern Abbey

c points to b; d is a pointer with the same value as b (*c).

==

Quote:

> But running the program prints "Abbey" which means that d points to
> 't' and d+5 is the string "Abbey" which means that d doesn't point to
> b but points to the SAME address that b does.

> HELP!! Thanks...Stephen.  ;>)

> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>

> main()
> {
>  char a[] = "Tintern Abbey";
>  char *b=a+3;
>  char **c=&b;
>  char *d=*c;
>  puts(d +5);
> }

> --

>      *
>    *-|-*      Stephen Levinson

>    *-|-*
>      *

> --

--

--

Sat, 10 Nov 2001 03:00:00 GMT
Pointers to Pointers to Char in C
Groovy hepcat Stephen Levinson was jivin' on Mon, 24 May 1999 21:25:08
GMT in comp.lang.c.
Pointers to Pointers to Char in C's a cool scene! Dig it!

Quote:
>    In the following code I'm confused about what d points to. Here's my
>(obviously wrong) thinking...
>b is a pointer to char which points to the 4th char of array a[]... ('t');

Yes, that's right.

Quote:
>c is a pointer to a pointer to char which is assigned the address
> of b...so it holds the address of b. So c points to b.

Also correct.

Quote:
>Then we come to my problem:  *d = *c. It seems to say that d (which
>is a pointer to char) is assigned the dereferenced value of c. In

Yes. That means d is assigned the value of what c points to, which
is b.

Quote:
>other words, it would seem that d and c both point to b.

No, no. d points to what b points to, since d equals b. It's like
this: c points to b, therefore *c (dereferenced c, or the thing c
points to) is b. Since d is initialised with *c, which is b, it's the
same as saying

char *d = b;

Quote:
>But running the program prints "Abbey" which means that d points to
>'t' and d+5 is the string "Abbey" which means that d doesn't point to
>b but points to the SAME address that b does.

Yep.

Quote:
> char a[] = "Tintern Abbey";
> char *b=a+3;
> char **c=&b;
> char *d=*c;

--

----- Dig the EVEN NEWER, MORE IMPROVED news sig!! -----

-------------- Shaggy was here! ---------------
http://aardvark.apana.org.au/~phaywood/
============= Ain't I'm a dawg!! ==============
--

Fri, 23 Nov 2001 03:00:00 GMT
Pointers to Pointers to Char in C

Quote:

> Hi everyone...

> pointers.

>     In the following code I'm confused about what d points to. Here's my
> (obviously wrong) thinking...

> b is a pointer to char which points to the 4th char of array a[]... ('t');

> c is a pointer to a pointer to char which is assigned the address
>  of b...so it holds the address of b. So c points to b.

> Then we come to my problem:  *d = *c. It seems to say that d (which
> is a pointer to char) is assigned the dereferenced value of c. In
> other words, it would seem that d and c both point to b.

> But running the program prints "Abbey" which means that d points to
> 't' and d+5 is the string "Abbey" which means that d doesn't point to
> b but points to the SAME address that b does.

> HELP!! Thanks...Stephen.  ;>)

> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>

> main()
> {
>  char a[] = "Tintern Abbey";
>  char *b=a+3;
>  char **c=&b;
>  char *d=*c;
>  puts(d +5);
> }

This could be rewritten equivalently

char a[] = "Tintern Abbey";
puts(&a[8]);

or, if you prefer

puts((a + 3) + 5);

since this is what is really happening.
--

Fri, 23 Nov 2001 03:00:00 GMT

 Page 1 of 1 [ 6 post ]

Relevant Pages