LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-28-2013, 09:59 AM   #1
atlantis43
Member
 
Registered: Feb 2013
Posts: 206

Rep: Reputation: Disabled
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
 
Old 09-28-2013, 10:19 AM   #2
273
Senior Member
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 3,393

Rep: Reputation: 794Reputation: 794Reputation: 794Reputation: 794Reputation: 794Reputation: 794Reputation: 794
I think your confusion is being caused because they swap the arguments around when they pass them to showArguments.
 
Old 09-28-2013, 10:22 AM   #3
atlantis43
Member
 
Registered: Feb 2013
Posts: 206

Original Poster
Rep: Reputation: Disabled
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.

Last edited by atlantis43; 09-28-2013 at 10:25 AM. Reason: clarification
 
Old 09-28-2013, 10:36 AM   #4
273
Senior Member
 
Registered: Dec 2011
Location: UK
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 3,393

Rep: Reputation: 794Reputation: 794Reputation: 794Reputation: 794Reputation: 794Reputation: 794Reputation: 794
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.
 
1 members found this post helpful.
Old 09-28-2013, 10:42 AM   #5
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,522

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Quote:
Originally Posted by atlantis43 View Post
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.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
&argc, &argv problem knobby67 Programming 3 12-14-2009 11:02 AM
gdb cannot display argc/argv? Nick_Battle Programming 3 01-12-2007 12:06 PM
how to pass argv[] & argc to functions? skie_knite007 Programming 2 05-13-2005 11:12 PM
main(int argc, char **argv) Longinus Programming 4 06-12-2004 07:22 AM
argc argv linuxanswer Programming 8 10-25-2003 07:54 PM


All times are GMT -5. The time now is 08:02 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration