Thread: Why is there a warning??

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    1

    Why is there a warning??

    Code:
    1 foo(const char **p) { }
    2 main(int argc, char **argv)
    3 {
    4 foo(argv);
    5 }
    //line 4: warning: passing argument 1 of 'foo' from incompatible pointer type

    If u take the strlen function which has the prototype

    Code:
    size_t strlen(const char *s);
    U get ur code compiled without getting any warning when u pass char* argument. So y is there a warning in this case?

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    see http://c-faq.com/ansi/constmismatch.html for a (fairly) full explanation
    DavT
    -----------------------------------------------

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    On another note... I've always seen main() defined as:
    Code:
    int main( int argc, char* argv[] )
    but it would seem more logical to define it as:
    Code:
    int main( int argc, const char* argv[] )
    Is there any downside to making main() const-correct?

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by cpjust View Post
    On another note... I've always seen main() defined as:
    Code:
    int main( int argc, char* argv[] )
    but it would seem more logical to define it as:
    Code:
    int main( int argc, const char* argv[] )
    Is there any downside to making main() const-correct?
    If want to make argv const, you should do this:
    Code:
    int main( int argc, const char * const *const argv )
    But From what somebody said in this forum a while back, it actually is ok to modify the argv array. However, the buffers involved are only guaranteed to be large enough to hold the data they originally contain.

    Also, I would imagine some compilers may require argv to be nonconst, since it it easier to have one valid argument list for main, than several.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    const char * const *const argv
    3 consts and a split double pointer?? What the hell is that?
    This I understand:
    Code:
    const char** const argv
    but why the 3rd const?

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by cpjust View Post
    Code:
    const char * const *const argv
    3 consts and a split double pointer?? What the hell is that?
    This I understand:
    Code:
    const char** const argv
    but why the 3rd const?
    The middle const is there to stop you modifying the pointers in the array, such as setting them to NULL, or swapping them.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM