Thread: int main (int argc, string& argv[]) ??

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    int main (int argc, string& argv[]) ??

    Never fear, it's another of ahluka's stupid ideas .

    Would it make a (noticable) difference if argv was a string&? Just a dumb idea I had. I'll go and try it now....
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Damn me. Arrays of references are illegal, as I'm kindly reminded by my compiler.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Interesting. I'll give you 3 guesses as to what happened...
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Code:
    #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;
    }
    pointer hell

    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?
    Last edited by major_small; 07-15-2005 at 02:29 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    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
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  6. #6
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    This is interesting, I tried the code above, and it worked. Now, I then tried this:
    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;
    }
    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.
    Last edited by cboard_member; 07-15-2005 at 03:04 AM.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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:
    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;
    }
    produce this output:
    Code:
    a:  0
    *b: 5
    *c: 5
    
    &a: 0xbffff5f4
    b:  0xbffff5f4
    c:  0xbffff5f4
    I'm guessing that's due to compiler optimization...
    Last edited by major_small; 07-15-2005 at 03:17 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by major_small
    I'm guessing that's due to compiler optimization...
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by ahluka
    This is interesting, I tried the code above, and it worked. Now, I then tried this:
    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;
    }
    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.
    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:
    Code:
    jshao@MCP ~/Programming/C++ $ ./test.exe a b c d
    ./test.exe
    a
    b
    c
    d
    using your code, the output should be:
    Code:
    jshao@MCP ~/Programming/C++ $ ./test.exe a b c d
    b
    don't forget, arrays start at 0, so to get the second element, you have to reference index 1
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    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].
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  12. #12
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    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
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  13. #13
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    » 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...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  3. I need help with variables, please
    By JOCAAN in forum C++ Programming
    Replies: 39
    Last Post: 12-08-2007, 04:16 PM
  4. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  5. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM