A quick question

This is a discussion on A quick question within the C++ Programming forums, part of the General Programming Boards category; When I start Geany and choose New>C++ Source, it starts with a pre-defined basic code like this: Code: #include<iostream> using ...

  1. #1
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321

    Arrow A quick question

    When I start Geany and choose New>C++ Source, it starts with a pre-defined basic code like this:

    Code:
    #include<iostream>
    using namespace std;
    
    int main(int argc, char** argv)
    {
    
        return 0;
    }

    What I've seen in books and tutorials is
    Code:
     int main(int argc, char* argv[])
    What's the difference b/w the two formats?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    There is no difference. A array decays to a pointer to its first element when passed as an argument. The only "difference" is that the C++ standard gives the latter, not the former, as the "canonical" example.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    Well, unfortunately, main is different from the rest. If you pass a 2D-array to a function, only a pointer is created. But to main, a pointer-to-pointer is passed, or pointer to an array, if you will.

    Therefore, both are syntactically correct.
    It's a dynamically allocated 2D array, and thus needs to be a pointer to pointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Well, unfortunately, main is different from the rest. If you pass a 2D-array to a function, only a pointer is created. But to main, a pointer-to-pointer is passed, or pointer to an array, if you will.
    I do not think the global main function is different at all, in this case, but rather that argv is not a two dimensional array, but an array of pointers to the initial characters of null terminated strings, including a terminating null pointer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    Which is exactly why I said it was different.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Which is exactly why I said it was different.
    No, you stated that it was different because argv is a two dimensional array, yet a pointer to a pointer, not just a pointer, was created. This is false, since argv is not a two dimensional array to begin with.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Delete the "using namespace std;" and only use it in more limited scopes, if at all.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    That is not necessary.
    That's preference. Mostly it's OK unless if working in large projects, where you might want to look out.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by Elysia View Post
    That is not necessary.
    That's preference. Mostly it's OK unless if working in large projects, where you might want to look out.
    Yes, for toy programs it's not a big deal, but it's distrubing that an IDE would use such a practice when generating code. It teaches bad habits and there's enough of that going around.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    The IDE does not generate such code to my knowledge.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Didn't the OP say that was the template program it created?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Yes, for toy programs it's not a big deal, but it's distrubing that an IDE would use such a practice when generating code. It teaches bad habits and there's enough of that going around.
    In this case, whether it is a bad habit or not is debatable. The author of that editor template could cite C++ Coding Standards item #59, where Sutter and Alexandrescu state that: "In implementation files after all #includes, you can and should write namespace using declarations and directives liberally. This is the right way to reconcile brevity with modularity."

    That using directive in that editor template certainly comes in an implementation file after the lone #include.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    Quote Originally Posted by medievalelks View Post
    Didn't the OP say that was the template program it created?
    I was thinking of something else... Another program that did not.
    The OP did say so... You were right, I had the wrong application in mind.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by laserlight View Post
    In this case, whether it is a bad habit or not is debatable. The author of that editor template could cite C++ Coding Standards item #59, where Sutter and Alexandrescu state that: "In implementation files after all #includes, you can and should write namespace using declarations and directives liberally. This is the right way to reconcile brevity with modularity."
    Yes, it's a toy program. It doesn't matter in this case. In larger programs, it can cause problems down the road. Cline, Lomow, and Girou discuss this in FAQ 15.07 of their "C++ FAQs" book:

    "The using directive is less desirable than the using declaration because it pollutes the global namespace unnecessarily and opens the door for later code breakage. This is because compiler writers and class library providers can add names to the standard namespace, which means that programs that utilize 'using namespace std'; can suddenly fail to compile even though the user has not changed anything."


    It's a bigger problem with 3rd party libs that usually change more often than the standard library.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Yes, it's a toy program. It doesn't matter in this case.
    By "in this case" I was referring to the placement of the using directive, not the fact this this is merely an editor template.

    In larger programs, it can cause problems down the road.
    I agree, but I am also persuaded by Sutter and Alexandrescu's argument that "the vast majority of the time there is no ambiguity".

    This is because compiler writers and class library providers can add names to the standard namespace, which means that programs that utilize 'using namespace std'; can suddenly fail to compile even though the user has not changed anything.
    I think that they have a point, but this is mitigated by the fact that such names can be fully qualified on the off chance that a name collision occurs.

    Incidentally, Sutter and Alexandrescu argue that using declarations at namespace level are not necessarily better than using directives at namespace level. Their counterexample is:
    Code:
    // snippet 1
    namespace A {
    	int f(double);
    }
    
    // snippet 2
    namespace B {
    	using A::f;
    	void g();
    }
    
    // snippet 3
    namespace A {
    	int f(int);
    }
    
    // snippet 4
    void B::g() {
    	f(1); // which overload is called?
    }
    EDIT:
    This may be much ado about nothing: Geany 0.11's C++ source template does not come with a using directive.
    Last edited by laserlight; 04-20-2008 at 10:11 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21