Re: More on pointers to pointers.




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

.



Relevant Pages

  • Re: Typecasting pointers
    ... array of shorts, and initialize them all to the same value. ... You're saving the value of ptr so you can freeit later. ... A pointer increment advances the pointer by one object size, ... optimizing compiler could have done a better job than you have. ...
    (comp.lang.c)
  • Re: char **argv & char *argv[]
    ... "pointer to pointer to char". ... >> pointer)) pointing to the first element of an array. ... so we have to start adding more context. ... type "pointer to char", rather than "array MISSING_SIZE of char". ...
    (comp.lang.c)
  • Re: Objects
    ... is ptr. ... value of the individual ints in the array. ... object of type int*. ... between an array and a pointer to 'type'. ...
    (comp.lang.c)
  • Re: Binary file from C++ to Matlab
    ... By convoluting array and pointer syntax, ... arr is an array of doubles but also an acronym for &arr. ... ptr is a pointer to a constant array literal. ...
    (comp.soft-sys.matlab)
  • Re: Binary file from C++ to Matlab
    ... By convoluting array and pointer syntax, ... arr is an array of doubles but also an acronym for &arr. ... ptr is a pointer to a constant array literal. ...
    (comp.soft-sys.matlab)