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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:28:09+00:00 2026-06-17T01:28:09+00:00

I have a main function, int main (int argc, char **argv) {} I’m supposed

  • 0

I have a main function,

int main (int argc, char **argv)
{}

I’m supposed to pass two different command-line arguments:

argv[1]=IP ADDRESS type
argv[2]=INTEGER type

For example, ./a.out IP-ADDRESS INTEGER. I am calling this binary with some other script.

The problem is while argv[1] is not available it behaves like INTEGER value as argv[1].

How can I put a check in that if argv is type INT don’t make it to the argv[1] or only IP-ADDRESS type is allow in argv[1]?

  • 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-17T01:28:10+00:00Added an answer on June 17, 2026 at 1:28 am
    int main (int argc, char **argv)
    

    is more commonly expressed as:

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

    that is, argv is a pointer to an array of chars. What those chars represent is up to you, but generally these are going to be zero-delimited strings when the program is called on the command-line.

    If the user does not supply positional parameters then maybe you should be using options. See getopt().

    For example:

    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    
    /* Options:
           -i : integer argument
           -p : ip address
    */
    int main (int argc, char *argv[])
    {
        int option;
        int integer = 0;
        char *ip_address = NULL;
    
        opterr = 0;
        while ( (option=getopt(argc, argv, "i:p:")) != EOF ) {
          switch ( option ) {
            case 'i': integer = atoi(optarg);
                      break;
            case 'p': ip_address = optarg;
                      break;
            case '?': fprintf(stderr,"Unknown option %c\n", optopt);
                      exit(1);
                      break;
          }
        }
    
        if (integer)    printf("INTEGER: %d\n", integer);
        if (ip_address) printf("IPADDRESS: %s\n", ip_address);
    
        return 0;
    }
    

    Sample runs:

    ./a.out -i 1234 -p 128.0.0.1
    INTEGER: 1234
    IPADDRESS: 128.0.0.1
    
    ./a.out -i 1234 
    INTEGER: 1234
    
    ./a.out -p 128.0.0.1
    IPADDRESS: 128.0.0.1
    
    ./a.out -p 128.0.0.1 -i 1234 
    INTEGER: 1234
    IPADDRESS: 128.0.0.1
    
    ./a.out -x stuff 
    Unknown option x
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a main function like this: int main(int argc, char **argv) {
I have this code below: char buffer[10]; void main(int argc, char *argv[]) { strcpy(buffer,
#import <UIKit/UIKit.h> int main(int argc, char *argv[]) { NSLog(@new event...); NSAutoreleasePool * pool =
Essentially, the int main( int argc, char** argv ) does exactly that (or to
vi example.c #include<stdio.h> #include flite.h register_cmu_us_kal(); int main(int argc,char** argv) { cst_voice *v; if(argc!=2)
I have a main thread that invokes a child thread function at different times
I have the following function int namecomp(char c); Part of the function code else
Hello ever one I have made a main function and two files one header
I have main.cpp (including main function) and func1.cpp, and I want to link these
I have a main function in javascript that is function a() { some code

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.