Re: More on pointers to pointers.
- From: "Chad" <cdalten@xxxxxxxxx>
- Date: 30 Mar 2006 20:36:38 -0800
Dave Vandervies wrote:
In article <1143773007.173184.158940@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
Chad <cdalten@xxxxxxxxx> wrote:
I suspect I'm missing a broader concept here.
It looks to me like the broader concept you're missing is actually
pointers and arrays, not pointers to pointers.
You may find further enlightenment at
http://www.c-faq.com/aryptr/index.html
and at
http://groups.google.com/groups?q=group:comp.lang.c+author:"chris+torek"+"the+rule"
Okay, given the following
code:
#include <stdio.h>
int main(void) {
char *ptr[] = {"Garbage", "test", "work"};
ptr is an array of pointer to char with a slightly confusing name (since
it isn't actually a pointer).
Like any other array, it will be converted to a pointer to its first
element in most places you'll use it.
char **arg;
...and arg is a pointer to pointer to char, which can point at any element
of ptr[] (in particular, the first one that you get when you just say
`ptr' in a value context).
arg = ptr;
This does:
Convert ptr (array of pointer to char) into a pointer to its first
element (with type pointer to pointer to char).
Store that value in arg.
printf("The string is: %s\n", ptr[1]);
return 0;
}
Why am I not allowed to do something like:
arg = &ptr; ?
Giving it to the & operator like you're doing here is one of the
places where an array name *doesn't* get converted to a pointer[1].
The right-hand side of this assignment is a pointer to the array (with
type "pointer to array 3 of pointer to char"), and you're trying to
assign it to a pointer to pointer; since there's not a defined way to
convert from one of those types to the other (and not really a sensible
way either), the compiler complains.
So in other words,
args = ptr;
could also be written as
args = &ptr[0]; ?
Chad
.
- References:
- More on pointers to pointers.
- From: Chad
- Re: More on pointers to pointers.
- From: Dave Vandervies
- More on pointers to pointers.
- Prev by Date: Re: Convert Java <long> to C <?>
- Next by Date: Re: Convert Java <long> to C <?>
- Previous by thread: Re: More on pointers to pointers.
- Next by thread: Re: More on pointers to pointers.
- Index(es):
Relevant Pages
|