arguments from another func

This is a discussion on arguments from another func within the C Programming forums, part of the General Programming Boards category; Hello, I've some like code... Which's method to choose second argument from command-line? Code: int main(int argc, char **argv){ int ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    77

    arguments from another func

    Hello,
    I've some like code...
    Which's method to choose second argument from command-line?
    Code:
    int main(int argc, char **argv){
    int a;
    a= atoi(argv[1]);
    func(len);
    }
    
    void func(int len){
    ...
    b=atoi(argv[2]); // how to do int?
    }
    If I'm declared argv in "func" it not would be way to do it. How to do it and set argument from command-line?
    ___
    Thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You will need to pass the content in argv[] to the func().

    I suppose you can do it about 6 different ways:
    1. Convert argv[2] to int inside main, and pass that to func().
    2. Pass a char * to func, and pass argv[2] as that argument.
    3. Pass a argc and char **argv (or char *argv[] if you prefer that notation - it's the same thing) to func().
    The further ways are somehwat less obvious and/or meaningful, so i'll stop there.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    77
    I think what use it without pointers is very better. Please, watch me sample this.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void testfunc (int x) {
    	printf("%d\n", x);
    }
    
    int main (int argc, char *argv[]) {
    	if (argc>2) testfunc(atoi(argv[2]));
    }
    Last edited by MK27; 01-06-2009 at 10:54 AM. Reason: #include <stdlib.h>
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,440
    atoi() is declared in <stdlib.h> so it would be a good idea to include that header.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    atoi() is declared in <stdlib.h> so it would be a good idea to include that header.
    oh yeah!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    77
    yes, but I need take another arguments from testfunc. And use it in my main function...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void testfunc (int x,int len) {
    	printf("%d\n", x);
    }
    
    int main (int argc, char *argv[]) {
    	if (argc>2) testfunc(atoi(argv[2]));
    
    testfunc(len); // error function has been more arguments, but I need use it.
    }
    But, If I was doing it, I took compilation error hereof what testfunc to take more arguments.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,440
    Quote Originally Posted by quantt
    But, If I was doing it, I took compilation error hereof what testfunc to take more arguments.
    That's because you defined testfunc() to take two arguments, but you only passed it one argument.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    77
    Quote Originally Posted by laserlight View Post
    That's because you defined testfunc() to take two arguments, but you only passed it one argument.
    Well, therefore I ask this question. How to provide argument to main function?

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Arguments to main are sent from the command line. These are typically called options or switches in documentation because they are usually not required (the program will behave in some specified, default way with no arguments) and they have some specified impact on how the program runs.

    Program options follow the name of the program when you invoke it on the command line.
    In your example you might type:
    cprog 432
    But that's not very useful. When it is useful, you know what the arguments mean.

    This demonstrates it is possible to do batch processing with C programs, though. Meaning, you can write a program that will process files rather than spending a period of time with the user doing data entry. This can be handy for repetitious work that a user has to do, especially. For example, a program that plots the average rainfall per month over a period of several years might do batch processing, because it would be tedious and prone to human error otherwise.

    Batch processing is not suitable for all programs, and knowing when to utilize it comes with experience. Usually though, if you have enough data to make a whole file, you can make a batch processing program. Even so, sometimes you will have to use data entry. Sometimes you will want to support both for user friendliness. A phone book program, for example, would have a user interface anyway even though you could make a file of new addresses and process those.

    There is a special version of main if your program has command line arguments.
    Code:
    int main ( int argc, char **argv )
    The argv parameter is a string array (as in argument vector), argc is it's length (the argument count). Together these things make it possible for you to process information pounded out on the command line. Each argument is received as-is, in a read only form, starting at position 1.

    The specifics of this data structure are covered in the FAQ, if you are still confused about something. This post is not meant to cover the entire discussion from the FAQ.
    Last edited by whiteflags; 01-07-2009 at 10:08 AM.

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by quantt View Post
    Well, therefore I ask this question. How to provide argument to main function?
    You mean how to update values of some var in the main function?
    or use return

    Code:
    int testfunc(int x)
    {
       return 2*x;
    }
    
    int main(void)
    {
       int x = 2;
       int y = testfunc(x);
       printf("Arg=%d, Res = %d", x,y);
       return 0;
    }
    Or - if you need to update more than one value - pass argument by pointer
    Code:
    void testfunc(int x, int* result)
    {
       *result = 2*x;
       return;
    }
    
    int main(void)
    {
       int x = 2;
       int y = 0;
       testfunc(x, &y);
       printf("Arg=%d, Res = %d", x,y);
       return 0;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    That is not the only error.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void testfunc (int x,int len) {
    	printf("%d\n", x);
    }
    
    int main (int argc, char *argv[]) {
    	if (argc>2) testfunc(atoi(argv[2])); //This is wrong also
    
    testfunc(len); // error function has been more arguments, but I need use it.
                          //You don't declare len
    }
    This is correct:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void testfunc (int x,int len) {
    	printf("%d\n", x);
    }
    
    int main (int argc, char *argv[]) {
    	if (argc>2) testfunc(atoi(argv[2]), 3);
    
    testfunc(2, 3);
    }
    But meaningless since you only use one parameter inside testfunc()
    In any case in testfunc() blue parameters will be x and dark orange ones will be len

  13. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    77
    g'day,
    right, but I've yet another question stilling that code.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    void testfunc (int x,int len) {
         len=10000;
            printf("%d\n", x);
    return (len);// warning: 'return' with a value, in function returning void
    }
    int main (int argc, char *argv[]) {
    int len;
            if (argc>2) testfunc(atoi(argv[2]), len );
    
    printf("%d\n", len);
    }
    Tell me please, how to take printf len value in main function when len value located in testfunc function?

    thanks in advance!

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,440
    Quote Originally Posted by quantt
    Tell me please, how to take printf len value in main function when len value located in testfunc function?
    Return the value or use a pointer.

    What is the testfunc function supposed to do? What is your whole program supposed to do?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Jan 2009
    Posts
    77
    Quote Originally Posted by laserlight View Post
    Return the value or use a pointer.

    What is the testfunc function supposed to do? What is your whole program supposed to do?
    Hm, but if I doing return the value then have got warning, also it doesn't work.
    Listen, I'm posted it in above.
    return (len);// warning: 'return' with a value, in function returning void

    Just I want getting and inserting value into one function and the next to use it in another functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  2. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  3. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM
  4. typedef a func
    By trekker in forum C Programming
    Replies: 4
    Last Post: 07-02-2002, 05:15 AM
  5. Delete & print func()
    By spentdome in forum C Programming
    Replies: 5
    Last Post: 05-29-2002, 09:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21