Thread: passing params to main()

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    10

    passing params to main()

    Hi,

    How do we pass a param to main() function .

    I have a main function which should accept a string as a param. This param comes from an appl which calls this C code ...

    something like this.

    main(char array / string)

    Is argc, argv only for params with coomand line i/p?....


    Also I know that we can have something like this

    int func1(int a)....thats without a main...

    Can we write a func without main ..?...

    How do I do this..
    thanks

  2. #2
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    you can write functions with different names but a program begins excution at main()
    if you dont have main the compile should give you an error

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You do not call the main function. It is a special function used as an entry point to your program. If you need to call the main function as a function, move everything in it to a separate function with a different name and call that.

    You can have functions defined without a main. People do that a lot when they create libraries full of useful functions but no main function to run them. Users of that library can call those functions in their programs, which will all have a main function themselves (or the equivalent). Just write the function and compile the file. You can't run any program, but the function is still there. You would probably want a test program that uses the function, and that test program will have a main(), but the file with your function itself doesn't require one.

    The compiler will not give an error if you don't provide a main function. The linker might, but you don't need to link unless you are trying to build an executable, and you'd need a main for an executable anyway.
    Last edited by Daved; 06-20-2005 at 12:32 PM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    10
    I need the executable...

    So I guess i will go ahead and put in the main ..

    How do we pass a string param to main ...

    I dont know if arc argv will be good...

    bcos this will be called from another appl...and theres no command line coming in here..

    Please give me some ideas.
    thanks

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    When you say this will be called from another application, what do you mean? To pass command line parameters to a program, you just pass them on the command line -

    myprog.exe Param1 Param2 "Param 3" 4 p5

    Your program gets these strings from the array of strings that is the second parameter to main (usually named argv). The argc variable tells you how many strings you have (how many command line arguments were passed in).

    To return a string to the outside program that calls your program, you will need more advanced techniques. You can only return an integer from a C++ program. One option is to write out a file with the string in it and have the outer program read that file. Another option is to use interprocess communication to communicate the information between the two programs. Based on your questions, these might be too advanced for you, but it's hard to tell without more information.

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    A little OT but interesting nonetheless.

    you'd need a main for an executable anyway
    Not strictly true. You can actually make do certainly on MSVC with a mainCRTStartup() or WinMainCRTStartup() for a windows program.

    An interesting article with information on this technique
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    10
    This C program will be called from an Oracle Form ...Some other person is taking care of it...I dont know much abt it ....
    However,
    That Form will send my C program a string ...And I am supposed to do the stuff and return a string....

    I am not concerned about how Oracle handles it...

    All I need is

    Code:
    main(string param) 
    
    {
       ----
    
      return str;
    
    }
    this is the only part I need to do and I need to fix.

    Thanks

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    > Not strictly true. You can actually make do certainly on MSVC with a mainCRTStartup() or WinMainCRTStartup() for a windows program.
    True. I was wondering if someone would call me on that, hence the "(or the equivalent)" disclaimer earlier in the post.

    mike11, in standard C or C++ you cannot return a string from your program. As I already said, you need to find another way. Also, my previous post indicated how to get the string param. Did you see that part?

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    10
    let me see if i can have the result go into a file and discuss if the appl can read it ..

    thanks

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    10
    Daved,

    Lets say I use a file to store the result and have the calling program to read it.....

    But how do I pass a string to the main

    The oracle form will do something like

    cfunc(str);

    how do i have my main function take string param as i/p...

  11. #11
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Sorry that is not standard C++ or C code. Try word instead of string and see where that gets you.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Is cfunc the name of your program executable? Is cfunc the name of a function in your code?

    There is a difference between calling a function in a library and running a separate program executable. IT is a very IMPORTANT distinction. You can pass and return a string from a function, but you cannot return a string from a C/C++ program.

    I am assuming that you have to run a separate program executable from the oracle form (I don't know about oracle forms, maybe somebody can shed light on how they call into C code).

    Here is an example of a C++ program:
    Code:
    #include <iostream>
    
    int main(int argc, char* argv[])
    {
        for (int i = 0; i < argc; ++i)
        {
            std::cout << argv[i] << std::endl;
        }
    }
    If you compile and link this code into an executable called (for example) test.exe, then run this from a command line:
    Code:
    > test hello "I am a string" LastString
    the following will be output:
    Code:
    test
    hello
    I am a string
    LastString
    This is because the program reads in each command line and prints out each string on a separate line. The first string is at argv[0] and is the name and possibly the path of your program. You can ignore this in your code. The second string in the array, argv[1], is the string passed to your executable. That is the one you will use. Does that make any sense?

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Maybe it is enough to output the string to stdout if oracle can redirect stdout and read from it.

  14. #14
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Why dont people just give examples of how they usually pass information between programs.

    A game would probably be using a server/client method using ports?
    A virus program might output the string to a file and then have another program read that file?
    A program might make the string then send the memory location to the other program to read? (but how to send that memory location)

    I've never done this but there have to be ways, and its not by using main() to return strings ;P
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    2
    Gurus,

    I went thro all the stuff that you guys suggested....

    Now I have one possiblilty ...Please could you tell me if this is gonna be good enough...

    Oracle can do file reading and writing....which C can also do.

    Lets say Oracle stores these parameters in a file called vdata100.txt

    Oracle can also read the file vdataresult100.txt.

    But since I cannot hard code these parameters, I have to pass them to C as variables....

    Oracle will call the C function and pass the parameter v_infile, v_outfile which are the names of the txts...


    Oracle says:
    Code:
     
     tmp = ...(C,datasubmit.c, ...)
    register.invar = v_infile   // param1
    register invar = v_outfile  // param1
    Can my C function look like this

    Code:
    databasubmit.c 
    
    int  send_info(var_infile, var_outfile) 
    
    {
      fp = fopen(var_infile) 
     ----
     write (var_outfile )
     return 0 for sucess / failure
    } 
    
    /* NO MAIN FUNCTION....... */
    Just compile it but dont link and create a DLL...and give to oracle...
    All this is in VC7

    Will this datasubmit.c work?....
    thanks a bunch

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a memory location back to main
    By jbsloan in forum C Programming
    Replies: 5
    Last Post: 03-07-2005, 12:17 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. void or int for main?:)
    By bigtamscot in forum C Programming
    Replies: 13
    Last Post: 09-27-2001, 03:11 AM
  5. passing a file name to main()
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 09-06-2001, 04:08 PM