Never fear, it's another of ahluka's stupid ideas :D.
Would it make a (noticable) difference if argv was a string&? Just a dumb idea I had. I'll go and try it now....
Printable View
Never fear, it's another of ahluka's stupid ideas :D.
Would it make a (noticable) difference if argv was a string&? Just a dumb idea I had. I'll go and try it now....
Damn me. Arrays of references are illegal, as I'm kindly reminded by my compiler. :rolleyes:
Interesting. I'll give you 3 guesses as to what happened...
pointer hell :DCode:#include<iostream>
#include<string>
int main(int argc, std::string*argv[])
{
for(int i=0;argv[i];i++)
{
std::cout<<reinterpret_cast<char*>(argv[i])<<std::endl;
}
return 0;
}
now if somebody could tell me why that works... (yes, I guessed... a few times...) I'm thinking that you can't put an std::string* out to the stream, but if you cast it to a char*, you trick ostream into thinking it knows what to do with it
if that's the answer, that would be understandable, but how/why is an std::string* safely castable to a char*? is it really safely castable, or am I getting lucky? One last thing: how does reinterpret_cast work? is it just taking a best-guess approach (my guess), or just trying to brute force a string* into a char*, or something else?
:confused:
Damn it was only yesterday I was reading something (by Bjarne himself) and he was explaining reinterprest_cast / static_cast / const_cast. It may be in this FAQ but I'm not sure:
http://www.research.att.com/~bs/bs_faq2.html
This is interesting, I tried the code above, and it worked. Now, I then tried this:
I don't understand why the loop was required. I gather it has something to do with pointer arithmetic? The above didn't work btw.Code:
#include <string>
#include <iostream>
int main (int argc, std::string* argv[])
{
//for (int i=0; argv[i]; i++)
std::cout << reinterpret_cast<char*> (argv[2]) << std::endl;
return 0;
}
well, in his FAQ he explains why you should use those over C-style casts and type(data) type casts, but it's mostly about readability...
one more (slightly off-topic) question:
why does this code:produce this output:Code:#include<iostream>
int main()
{
const int a=0;
int*b;
int*c;
b=const_cast<int*>(&a);
*b=5;
c=const_cast<int*>(&a);
std::cout<<"a: "<<a<<"\n*b: "<<*b<<"\n*c: "<<*c<<"\n\n&a: "<<&a<<"\nb: "<<b<<"\nc: "<<c<<std::endl;
return 0;
}
I'm guessing that's due to compiler optimization...Code:a: 0
*b: 5
*c: 5
&a: 0xbffff5f4
b: 0xbffff5f4
c: 0xbffff5f4
Looks like it to me. When compiling, it sees that a is a const and when it gets to the cout statement and sees that you are trying to print out a it probably just does a replacement of a with 0. When you print out *b and *c however it actually needs to go to the memory location and see what's there before it can be printed.Quote:
Originally Posted by major_small
that should work if you gave it two command-line options. my loop just went through everything on the command line. for examle, my output was:Quote:
Originally Posted by ahluka
using your code, the output should be:Code:jshao@MCP ~/Programming/C++ $ ./test.exe a b c d
./test.exe
a
b
c
d
don't forget, arrays start at 0, so to get the second element, you have to reference index 1Code:jshao@MCP ~/Programming/C++ $ ./test.exe a b c d
b
I feel so stupid. That was the problem - why in the name of all things holy did I try to outout argv[2]? Damn me. It must the mountain air lol. Yes it works now - given I change it to argv[1].
If you read Stroustrup's FAQ it will also say that int main (int argc, std::string* argv[]) is illegal. I hope you just doing this for fun or because you're curious, otherwise it is wrong and shouldn't be used.
Lol, yeah I was just doing it for fun. I dunno why but I woke up this fine morn and thought 'I wonder what'll happen if I make argv[] a string?'.
I'm weird that way :D
» If you read Stroustrup's FAQ it will also say that int main (int argc, std::string* argv[]) is illegal. I hope you just doing this for fun or because you're curious, otherwise it is wrong and shouldn't be used.
not that I'd use it anyway, I would like to see where in his FAQ you found that... I just did a quick search and couldn't find anything about it...
I couldnt find it in Stroustrup's FAQs either.
Still, I suppose one could define an alternative main function that uses a std::string std::vector and which is called by the real main() function that takes null-terminated multibyte strings for argv.
Ok, bad memory. The FAQ and the standard say only that int main(int argc, char* argv[]) must be allowed. My memory made me think that parameters different than those two were illegal unless they came after those two, which is what made me say that the string version was illegal. Instead, the standard only recommends that further parameters be added after argv.
Even if implementations are allowed to allow stuff like that, that doesn't mean they do, and unless your implementation specifically states that it does allow it, it is still "illegal" and not a good idea, as I'm sure you all know already.