Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8120135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:01:43+00:00 2026-06-06T05:01:43+00:00

I have asked a similar question about command line arguments in C++ some hours

  • 0

I have asked a similar question about command line arguments in C++ some hours ago. Now I have another problem, as far as I know, command line arguments will be saved like a string in argv array. so comparing theme with a string should be logical, but it does not work in the way I want, look at this code:

#include <iostream>
using namespace std;

int main(int argc,char** argv)
{
    if (argv[2]=="stack") cout << "right";
    cout << argv[2];
    return 0;
}

now I pass this command to my compiled application named zero.exe;

zero.exe stack

the output should be “rightstack”, but if command will skip and only cout << argv[2]; will execute, so only stack will be shown on monitor. it shows "stack" is saved into argv[2], so if (argv[2]=="stack") should work, but it is not. where is the problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T05:01:46+00:00Added an answer on June 6, 2026 at 5:01 am

    For historical reasons, the arguments are passed as C-style strings; that is, each is a pointer to an array of characters, with a zero-valued character to mark the end. Similarly, a string literal (like "stack") is a simple array of characters.

    Your code compares two pointers, which will not be equal even if the string values are equal. To compare the strings, either convert one (or both) to std::string:

    #include <string>
    
    std::string arg2(argv[2]);
    if (arg2=="stack") std::cout << "right\n";
    

    or use the C library function for comparing C-style strings; this might be more efficient, but is also harder to read:

    #include <cstring>
    
    if (std::strcmp(argv[2], "stack") == 0) std::cout << "right\n";
    

    Also, the arguments are counted from 1, with the program name as argv[0], so you probably want to be testing argv[1] rather than argv[2].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Somebody asked similar question not long ago. But nobody answered comprehensively. Assume I have:
A similar question was asked and answered about a year ago, but was either
I have asked a similar question 6 months ago that still has not been
I asked a little while ago a question about rollback, I have a new
As I have asked the similar question about servlet ,which is for showing a
Another Newbie question in XSLT transformation. (I have asked similar question before, but in
Some days ago I asked a question about tagging differencies in 2 text files,
I know similar questions have been asked, but I'm not sure about the answers
I have asked similar question for Linux RPM ( Adding License Agreement in RPM
I have asked a similar question previously, but posting a new one as I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.