LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 10-25-2003, 06:00 PM   #1
linuxanswer
Member
 
Registered: Oct 2003
Distribution: woodY 3.0 stable
Posts: 61

Rep: Reputation: 15
argc argv


's there anyone who explain me the meaning of

if (argc != 2) //why different from 2? ; // 2 what's it?
usage(argv[0]); /which means that?

x = atoi(argv[1]) - 1; / what represents argv[1]?

or also


if (argc < 2) { |
fprintf (stderr, "usage: %s <binary> [output]\n\n", argv[0]); |
|
exit (EXIT_FAILURE); |
}
pathname = argv[1]; / i do not understand what indicates argv[1]
if (argc >= 3)
output = argv[2]; //

finally sorry but repeat, do not understand the usage of argc and argv in this way
 
Old 10-25-2003, 06:28 PM   #2
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
argc and argv are the variables that C and C++ use for passing arguments to a program on the command-line. If you look at the beginning of the main() function, there should be something along the lines of:

Code:
main( int argc, char ** argv )
argc is an integer that contains the number of arguments passed to the program. argv is an array of character arrays which contains the value of each argument. So, say your program is called 'foo', and you run it by typing:

foo a b c

Then argc is equal to three (since there are three arguments, separated by spaces), and argv contains the following:

argv[0]: "foo"
argv[1]: "a"
argv[2]: "b"
argv[3]: "c"

So, to answer your question, when the program compares argc to some number like 2, it's asking "did the user give enough arguments to run the program correctly? If not, print a message about how to use the program correctly."

Did all that make sense?

To understand it better, you could try writing a simple program to print the values of arguments. Try this (in C++):

Code:
int main( int argc, char ** argv )
{
  for ( int i = 0; i <= argc; i++ )
    cout << "argv[" << i << "]: " << argv[i] << endl;
  return 0;
}
Compile and run this with various arguments to see what happens.

Last edited by wapcaplet; 10-25-2003 at 06:29 PM.
 
Old 10-25-2003, 06:54 PM   #3
linuxanswer
Member
 
Registered: Oct 2003
Distribution: woodY 3.0 stable
Posts: 61

Original Poster
Rep: Reputation: 15
and the envp what represents in term of foo a b c ?
 
Old 10-25-2003, 07:01 PM   #4
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
I'm not sure what you mean. What's envp?
 
Old 10-25-2003, 07:09 PM   #5
linuxanswer
Member
 
Registered: Oct 2003
Distribution: woodY 3.0 stable
Posts: 61

Original Poster
Rep: Reputation: 15
for example

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

ecc. ecc.

if you approach it to the previous example of "foo a b c " envp[] now what represents?
 
Old 10-25-2003, 07:11 PM   #6
Kurt M. Weber
Member
 
Registered: Oct 2003
Distribution: Slackware
Posts: 335

Rep: Reputation: 36
envp is a pointer to the environment...
 
Old 10-25-2003, 07:21 PM   #7
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
Ah, I've never seen that before. A google search turned up an answer. Presumably this takes in various environment variables, such as the things you see if you type 'set' in a terminal window.
 
Old 10-25-2003, 07:40 PM   #8
linuxanswer
Member
 
Registered: Oct 2003
Distribution: woodY 3.0 stable
Posts: 61

Original Poster
Rep: Reputation: 15
Kurt M. Weber any examples and a more precise definition ?
 
Old 10-25-2003, 07:54 PM   #9
Kurt M. Weber
Member
 
Registered: Oct 2003
Distribution: Slackware
Posts: 335

Rep: Reputation: 36
Well, it's a pointer to an array. Basically, it's an array of strings (which means a two-dimensional array, since a string is itself an array), and each string contains a different environment variable.
 
  


Reply



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
how to pass argv[] & argc to functions? skie_knite007 Programming 2 05-13-2005 11:12 PM
Question about argv[] ghoughto Programming 6 10-25-2004 09:38 AM
main(int argc, char **argv) Longinus Programming 4 06-12-2004 07:22 AM
argv[] help wanted. (In C) vexer Programming 9 11-17-2003 05:09 AM
Help with argv pilot1 Programming 7 08-23-2003 02:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:03 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration