Question on argv and array of char string

Question on argv and array of char string

Post by Loui » Sat, 14 Mar 2009 15:39:16


Hi Guys:

I'm trying to understand argv in main, and i do understand it is an array of
character string. but when i try this its return error:

int main(int argc,char* argv[])
{
char* arofstr[] = {"foo", "bar", "baz"};


argv++; // i can do this
arofstr++; // i had compile error ISO C++ forbids cast to
non-reference type used as lvalue

cin.get();
return 0;
}

I thought arofstr is the same as argv since they are both a pointer to a
char array or array of char string.

Can anyone help me out Please!?

Thanks
Louis
 
 
 

Question on argv and array of char string

Post by tpl » Sat, 14 Mar 2009 18:59:29

"Louis" < XXXX@XXXXX.COM > writes:


I suspect that you do understand it. Your problem's with arofstr[].
Even the simpler
char arofstr[] = {'f','o','o'};
arofstr++;
would fail, because arofstr can't be changed.

 
 
 

Question on argv and array of char string

Post by Juha Niemi » Mon, 16 Mar 2009 00:29:57


There's a very simple way of demonstrating that argv and arofstr in
the given example are definitely *not* of the same type: Print the
values of sizeof(argv) and sizeof(arofstr).
 
 
 

Question on argv and array of char string

Post by James Kanz » Mon, 16 Mar 2009 18:58:40


Or typeid( argv ).name() and typeid( arofstr ).name(), if your
implementation has a usable implementation of typeid. (Most
compilers do---g++ seems to be the exception.) Good idea.

James Kanze (GABI Software) email: XXXX@XXXXX.COM
Conseils en informatique orient objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sard, 78210 St.-Cyr-l'ole, France, +33 (0)1 30 23 00 34
 
 
 

Question on argv and array of char string

Post by Loui » Tue, 17 Mar 2009 09:12:49

Hi James:

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, and do ptr++, it will work, is
that means,when i pass the array to the function test, 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?
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;

Thanks!

Louis







Or typeid( argv ).name() and typeid( arofstr ).name(), if your
implementation has a usable implementation of typeid. (Most
compilers do---g++ seems to be the exception.) Good idea.

James Kanze (GABI Software) email: XXXX@XXXXX.COM
Conseils en informatique orient objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sard, 78210 St.-Cyr-l'ole, France, +33 (0)1 30 23 00 34
 
 
 

Question on argv and array of char string

Post by James Kanz » Tue, 17 Mar 2009 17:53:32


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.


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.


Exactly.



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.

James Kanze (GABI Software) email: XXXX@XXXXX.COM
Conseils en informatique orient objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sard, 78210 St.-Cyr-l'ole, France, +33 (0)1 30 23 00 34