Thanks for your reply,i kind of getting the idea, and i try another test:
void test(char * ptr[])
{
cout << *ptr<< endl;
ptr++; // this work!
cout << *ptr << endl;
}
int main(int argc,char* argv[])
{
char* temp[] = { "foo", "woo", "wtf"}; //local
test(temp); //passing to funciton test
cin.get();
return 0;
}
If i pass the local array to the function,
You can't pass an array (local or otherwise) to a function. C++
doesn't allow it. There's no way to declare a function which
takes an array as an argument. You *can* pass a reference to an
array, a pointer to an array, or (what is the traditional way
from C), a pointer to the first element of an array. When you
write:
void test( char* ptr[] )
you are writing a lie. The type of the function is:
void test( char** ptr ) ;
This is a well known problem with C: arrays in C are broken.
The C++ standardization committee did consider the possibility
of "fixing" them, or at least making them less broken, but in
the end, decided that anything that they could do would only
break C compatibility, without really fixing them, and that
std::vector was an adequate replacement. Experience has shown
that this isn't 100% true; there are a few cases where the C
style array is still needed. Boost addressed most of these with
boost::array, which became tr1::array, and will be std::array in
the next standard.
and do ptr++, it will work, is
that means,when i pass the array to the function test,
It means that you're not passing an array to the function test.
Try
void test( char* (&array)[ 3 ] )
(Note that in this case, the size is part of the type.)
Better yet, get an implementation of tr1::array, and use it.
which eq char * ptr[] = temp, which result assgin a ptr to the
1st element of temp, and by increment ptr, i'm actually
increment the ptr, not the array temp itself and that's why it
is working?
Exactly.
which also imply if i do all this in local main, it also work
because im assign a ptr to a ptr of char to the temp array and
it is the same as the example above?
char ** ptr = temp;
cout << *ptr << endl;
ptr++;
cout << *ptr << endl;
Yes. Pointers are funny beasts in C++ as well (for historical
reasons). Practically speaking, unless you're writing really
low level code (like memory management), you should avoid
pointer arithmetic.