Thursday 14 June 2012

Argument passing to main() function explained

I have seen many people put up videos tutoring about c programming and stuff,
but most of those videos lack some of the major concepts important for practical
programming.
So i thought why not cover this topic too, this is also an outcome of requests from friends, having problems in same topic.

A prior knowledge of string constants, argument passing, pointers, pointers to pointers is necessary in any programming language, helps you to get eased up for it, if you know c then its even better,

lest start with a simple code that is so popular that even a non programmer who ever wanted to learn programming but lost interest must have also seen before throwing the book, yeah u got me right its the oldskool HELLO WORLD eg

+--------------------------------------------------------------------------------------------------------------------------------+

#include <stdio.h>

main(){

printf("hello world");
}

+---------------------------------------------------------------------------------------------------------------------------------+

when you compile this code snippet, you get the usual hello world output on screen, but this is constraining our will to program the way we want, in this example hello world string will get hard coded into the executable generated after compiling.

One question arises commonly, why cant it be dynamic??
ans is simple, it can be made dynamic, creators of c were not so foolish to leave such a thing. This can be achieved with parameter passing just like any other function, but the only difference between this type of argument passing is that arguments are passed from the enviroment of your os not inside your program.

With a slight modification in the code above you can achieve it,

+--------------------------------------------------------------------------------------------------------------------------------+

#include <stdio.h>

main(int argc, char *argv[])
// sometimes also seen as main(int argc, char **argv)
{

printf("%s",argv[1]);
}

+--------------------------------------------------------------------------------------------------------------------------------+

now the explanation bit,

structure of your main has changed to

main(INT, CHAR POINTER TO STRING POINTERS)

   }


lets start with the first argument

argc --> its an integer data type,
        --> keeps the count of total arguments

in a standalone program with no parameters, vaue of argc is always 1,
this facility can be used for error checking
IF (parameters less than 2)
PRINT plz check the usage or plz pass correct number of arguments.

this variable increments with your no of parameters you pass.

lets say your compiled program is helloworld.

so on a linux box

$./helloworld hello this is a parameter passing example

or a windows machine

>helloworld.exe hello this is a parameter passing example

value of argc will increment this way

+-----------------------------------------------------------------------
| execution| helloworld| this| is | a | parameter
|----------------+------------------------------------------------------
| argc         |          1      |  2   | 3  | 4      5
+-----------------------------------------------------------------------

and goes on.

now lets get onto next parameter,

char *argv[] or **argv


--> this is a pointer to an array of pointer

this is the portion where it gets confusing for some people, but i will start from the base and we can build our way up,

so the concept of an array

what is an array??

it is a contiguous block of memory which can be used to store data in a rightly order.
main points about it.

--> array name is a pointer itself,
basically it points to the first memory block of the array.

so this code is also fairly legal,

-------------------------------------------------------------------
int *p , egarray[2];
 p = egarray;
//then
printf() for egarray[0] and *p will output the
same value.
----------------------------------------------------------------------

 arrays can be created for all possible datatypes depending on your needs, int ,char n struct arrays are common in and same is with array of pointers.

think of it as a registers which stores the starting point of another array

pointer to pointer can be explained like this,

array ar;
                      ar0 | p1| -->q1
                      ar1 | p2| -->q2
                      ar2 | p3| -->q3
                      ar3 | p4| -->q4
                      ar4 | p5| -->q5

and these q pointers can be anything,,
but in this case we are talking about parameter passing which generally is in form of a statement/string.

so these q are all themselves  in  a form of

q="your parameter for the no. respectively";

so i hope this clears out the two parameters of main()

i have also uploaded a video to show you a jist of it, for gaining practical
confidence,, here.


there is one more minute query left which people get stuck with it, dont worry i will cover that too,

why is thr no " " in system() while using argv[]????

simple, these functions also take input as a string constant, which argv[] supplies to them as a pointer leading to it.

thanks for reading,
i hope this clears out some clouds for you.
B-)

No comments:

Post a Comment