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 6991669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:32:00+00:00 2026-05-27T19:32:00+00:00

we declare main() as int main(int argc, char *argv[]) and pass some argument by

  • 0

we declare main() as

int main(int argc, char *argv[])

and pass some argument by command line and use it

as argv[1], argv[2] in main() function definition but what if i want to use that in some other function’s definition ?

one things i can do it always pass that pointer char** argv from main() to that function by argument. But is there any other way to do so?

  • 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-05-27T19:32:01+00:00Added an answer on May 27, 2026 at 7:32 pm

    In order to make data available to other functions, you need to pass it as a parameter, or make it available through a global (not recommended) or a static variable.

    static char** args cmd_args;
    static int cmd_arg_count;
    
    int main(int argc, char** argv) {
        cmd_arg_count = argc;
        cmd_args = argv;
        do_work();
        return 0;
    }
    
    void do_work() {
        if (cmd_args > 1) {
            printf("'%s'\n", cmd_args[1]);
        }
    }
    

    The best approach is to make a function that parses command line parameters, and stores the results in a struct that you define specifically for the purpose of representing command line arguments. You could then pass that structure around, or make it available statically or globally (again, using globals is almost universally a bad idea).

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

Sidebar

Related Questions

Hey guys, suppose the following code: int main (int argc, const char * argv[])
Suppose you have the following code: int main(int argc, char** argv) { Foo f;
In Objective-C, why argv[] is declared as const: int main(int argc, const char *argv[])
File name: widgets.c #include <QtGui> int main(int argc, char *argv[]) { QApplication app(argc, argv);
I declare a static char array, then I pass it to a function. How
Code: #include <iostream> using namespace std; #define ADD(x,y) ((x)+(y)) int main( int argc, char**
I'm trying to declare a method in main.h like this: void buildGraph(int gNum, Graph**
I'm trying to declare a variable in an if-else block as follows: int main(int
I have the following code: template<int k> void foo() { } int main(int argc,
The following code is generating a stack overflow error for me int main(int argc,

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.