Results 1 to 3 of 3

Thread: simple question on pointer-arrays

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default simple question on pointer-arrays

    hi, I coded this:
    Qt Code:
    1. int main (int argc, char* argv[]) {
    2. cout << argv[1] << endl;
    3. if (argv[1] == "hi") cerr << " hello\n";
    4. char* p = "hi";
    5. if (p == "hi") cout << "hello\n";
    To copy to clipboard, switch view to plain text mode 
    at console I need to launch program so: program.exe hi
    and I don't understand why cout prints 'hi' but the first if isn't executed. isn't argv an array of char* ??? then why then second if is executed???
    thanks
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: simple question on pointer-arrays

    You are comparing pointers not strings. Either compare strings using strcmp() or use std::string instead of char *.

  3. #3
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: simple question on pointer-arrays

    Quote Originally Posted by jacek View Post
    You are comparing pointers not strings. Either compare strings using strcmp() or use std::string instead of char *.
    Exactly. argv is char**, so the = operator will compare the memory address of your "hi" with the memory address of argv[1]. And it would be a shitload of (bad) luck if these two addresses are the same
    "If you lie to the compiler, it will get its revenge." - Henry Spencer

Similar Threads

  1. QTextEdit simple question
    By Marcopolo in forum Qt Tools
    Replies: 4
    Last Post: 11th October 2007, 00:01
  2. simple thread layout question
    By mhoover in forum Qt Programming
    Replies: 1
    Last Post: 12th August 2006, 11:02
  3. simple pointer question
    By mickey in forum General Programming
    Replies: 6
    Last Post: 16th June 2006, 09:19
  4. simple question on Class-Members
    By mickey in forum General Programming
    Replies: 7
    Last Post: 4th February 2006, 22:37
  5. QTextEdit Qt4: simple question
    By TheKedge in forum Qt Programming
    Replies: 4
    Last Post: 18th January 2006, 12:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.