Monday, October 4, 2010

Passing into a compiled C program

Just figured out how to use the "argv" argument in C.

Pretty much it's whatever you type in the command console and it becomes stores in the array "argv" as an array. So "argv" is an array of arrays (or so it seems), not of individual characters as some websites might lead you to believe.

For example if I had this function in the file hello.c:

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

and I wanted to use some text to be inputed at the execution of the program, like reading "byebye" in the command "./hello byebye", I would do this:

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

somereadfunction(arg[1]);
}


This reads the second array of the array "argv" which is the "second" thing you wrote in the command prompt. The first would be "arg[0]" which is "./hello".

Try it, I used printf to confirm my suspicions.