Thread: How to Pass Main Fuction argument to some Other Function

  1. #1
    Registered User
    Join Date
    Dec 2014
    Location
    Mumbai, Maharashtra, India, India
    Posts
    14

    How to Pass Main Fuction argument to some Other Function

    Hello ,
    I am writing a program in which a Fucntion has to be wriiten to parse the Command Line .

    When I include Code for parsing in main fuction iteslf ,its run ok .
    But I want to make a fucntion of that code and call it from main ,than it show Segmentation error .

    By using Debugging I found Some thing is mess with " -m" Parameter of Command line , But Cant Rectify it ..
    Please Help !!

    Code:
    int main (int argc, char *argv[])
    {
             
       //get_parameter_value(argc,argv);    
       //  buffer[packet_size+1]= char ("\0");    
          while (argc > 1)
         {
        if (argv[h][0] == '-')
          {
            switch (argv[h][1])
            {
            case's':
                server = gethostbyname(argv[h+1]);
                    break;
                    
             case'c':
                break;
                                            
                   case'p':
                portno = atoi(argv[h+1]);                                      
            break;
    
                 case'm':
                mode= atoi(argv[h+1]);                                      
            break;
    
                case 'z':
                packet_size=atoi(argv[h+1]); 
                buffer= malloc(sizeof(char) *packet_size);
                
                                          
            break;    
           
                case'T':
                Sen_Loop_time=atof(argv[h+1]);                               
               break;        
                
             case'P':
             protocol=*(argv[h+1]);                 
                break;    
    
                      default:
                     printf("Wrong Argument: %s\n", argv[0]);
                     break;
          }  }
          h ++;
          argc--;
          }
        if (protocol=='t')
        sockfd = create_tcp_socket();
        
         if (protocol=='u')
        sockfd = create_tcp_socket(); 
         bzero((char *) &serv_addr, sizeof(serv_addr));   
      if(mode==1)
    {
    Send();
    print_sender_result();
    }
    else if (mode==0)
    {
    Receive();
    print_receiver_result();
    }
    return 0; 
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    main() needs to pass argc and argv to the called function. The called function obviously needs to accept such arguments.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Dec 2014
    Location
    Mumbai, Maharashtra, India, India
    Posts
    14
    Following is the receving Fuction declartion but gives segmentation error
    Code:
    void get_parameter_value(int argc,char *argv[])

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by SimplySud View Post
    Following is the receving Fuction declartion but gives segmentation error
    Code:
    void get_parameter_value(int argc,char *argv[])
    How is the function defined and called. A segmentation fault does not occur in a function declaration. Please show us the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-09-2012, 06:41 AM
  2. Replies: 2
    Last Post: 03-19-2012, 08:44 PM
  3. How to pass an argument into a function?
    By mykolg in forum C Programming
    Replies: 13
    Last Post: 11-22-2010, 12:12 PM
  4. How to pass a matrix/array from main to a function
    By Inukami in forum C Programming
    Replies: 7
    Last Post: 12-09-2009, 09:03 PM
  5. How to pass an array of strings as a function argument?
    By Nazgulled in forum C Programming
    Replies: 7
    Last Post: 04-02-2007, 10:38 AM