LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   argc and argv confusion (https://www.linuxquestions.org/questions/programming-9/argc-and-argv-confusion-4175478879/)

atlantis43 09-28-2013 09:59 AM

argc and argv confusion
 
Hope that someone can help clarify my confusion with the above fn parameters.
In a text I'm reading, the following is stated:
"The argc argument is an int that indicates the number of arguments, and the argv argument is an array of char pointers."
A code example follow to illustrate this point:

Code:

#include <stdio.h>

void showArguments(char *arg, int argNum);

int main(int argc, char *argv[]){
int i;
for(i=0;i<argc;i++){
  showArguments(*(argv+i), i+1);
}
return 0;
}

void showArguments(char *arg, int argNum){
printf("argument [%d]: %s\n", argNum, arg);
}

I am confused by the fact that in the called function, what I would consider argc is "char *arg", and argv is now "int argNum", which are the reverse data types that I would have expected from what the introductory text says about these arguments.
Hope that someone can understand the basis of my confusion, and help me understand this aspect of C programming

273 09-28-2013 10:19 AM

I think your confusion is being caused because they swap the arguments around when they pass them to showArguments.

atlantis43 09-28-2013 10:22 AM

Is it only in Main() that argc and argv are specified in the way mentioned? I have tried to switch the order of arguments in ShowArguments(), but that does not compile.

273 09-28-2013 10:36 AM

Yes it is only in main they are that way around -- in any other function/method the person writing the code is at liberty to assign the arguments they wish in the order they wish but main is a special case and only accepts specific arguments in a specific order.
I think the person who wrote the code you are looking at may have been trying to tell the reader that the order of arguments isn't relevant outside of main but caused confusion in the process.

dwhitney67 09-28-2013 10:42 AM

Quote:

Originally Posted by atlantis43 (Post 5036429)
Is it only in Main() that argc and argv are specified in the way mentioned? I have tried to switch the order of arguments in ShowArguments(), but that does not compile.

Your original example is correct; there's no need to swap anything. 273 was merely indicating that you were probably confused when the function is called. The string is passed as the first argument, then an index.

How about a clearer program...

Code:

#include <stdio.h>

void showArgument_style1(char* arg, int argNumber);    /* function prototype */

void showArgument_style2(int argNumber, char* arg);    /* function prototype */

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

    for (argNum = 0; argNum < argc; ++argNum)
    {
        char* arg = argv[argNum];

        showArgument_style1(arg, argNum);

        showArgument_style2(argNum, arg);
    }

    return 0;
}

void showArgument_style1(char* arg, int argNumber)
{
    printf("argument[%d]: %s\n", argNumber, arg);
}

void showArgument_style2(int argNumber, char* arg)
{
    printf("argument[%d]: %s\n", argNumber, arg);
}

What the author of the tutorial is trying emphasize is that you can pass arguments to a function, but these must be done in the order specified by the function prototype. You should also think of main() and printf() as functions too! These accept parameters similar to other functions, although printf() is a special case that accepts a variable number of parameters.


All times are GMT -5. The time now is 04:56 PM.