LinuxQuestions.org
Help answer threads with 0 replies.
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-13-2005, 10:32 AM   #1
sachitha
Member
 
Registered: Aug 2003
Location: Sri Lanka
Distribution: Redhat 9.0
Posts: 104

Rep: Reputation: 15
Unhappy argv[1]++ operation and sort


in a C program what does the argv[1]++ operation do???
as in like in the argv++ it incements the argument value..what does argv[1]++ do?
in the command line argument set below: if i give..
sachitha such indee
after the argv[1]++ operation how do i calculate the value of argv[1] [3] for example..

in perl how do u sort an array??
@num=(32,4,16,2);
@z=sort{$b<=>$a}@num; //does this sort @num in descending order or ascending order?? and i'm not clear about what the $b and the $a are???
pl help me clear my confusions...
thanks in advace!
 
Old 10-13-2005, 03:30 PM   #2
koodoo
Member
 
Registered: Aug 2004
Location: a small village faraway in the mountains
Distribution: Fedora Core 1, Slackware 10.0 | 2.4.26 | custom 2.6.14.2, Slackware 10.2 | 11.0, Slackware64-13
Posts: 345

Rep: Reputation: 33
The function main can be called with two arguments. The first (conventionally called argc, for argument count) is the number of command-line arguments the program was invoked with; the second (argv, for argument vector) is a pointer to an array of character strings that contains arguments, one per string.
By convention argv[0] is the name by which the program was invoked, so argc is atleast one.

So argv is a pointer to an array of pointers
argv[i] or *(argv + i) is a character pointer that points to the first character of the i+1 th command line argument.
and *argv[i] is the charcter it points to


Let us say you invoke a program "program" like this :

Code:
koodoo@knapsacker:~$ ./program sachitha such indee
Code:
argv--------------->| argv[0] |-------> ./program
                    | argv[1] |-------> sachitha
                    | argv[2] |-------> such
                    | argv[3] |-------> indee
                    |    0    |
So argv[1] points to s in sachitha and printf("%s", argv[i]) would print sachitha. If you do argv[1]++, argv[1] will now point to a in sachitha and the string output would now be achitha.
Note however the difference between the prefix and postfix notations of the ++ operator.
printf("%s", ++argv[i]) : argv[1] points to a in sachitha and achitha gets printed while in,
printf("%s", argv[i]++) : argv[1] points to a in sachitha and sachitha gets printed

Now argv[1][3] is the (3 + 1)th character in the string pointed to by argv[1]. Note argv[1][3] is the actual character and not a pointer to char
So initially argv[1] points to s in sachitha and argv[1][3] is h. If we do argv[1]++ then argv[1] points to a in achitha and argv[1][3] is i.

I do not know anything about perl so I cannot answer your second querry.
Hope this helps.

Last edited by koodoo; 10-13-2005 at 03:34 PM.
 
Old 10-14-2005, 11:16 AM   #3
sachitha
Member
 
Registered: Aug 2003
Location: Sri Lanka
Distribution: Redhat 9.0
Posts: 104

Original Poster
Rep: Reputation: 15
Thank you!
 
  


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
Question about argv[] ghoughto Programming 6 10-25-2004 09:38 AM
argv[] help wanted. (In C) vexer Programming 9 11-17-2003 05:09 AM
argc argv linuxanswer Programming 8 10-25-2003 07:54 PM
Help with argv pilot1 Programming 7 08-23-2003 02:20 PM
'delete[] argv': is this correct?? Disruptor Programming 4 07-10-2003 11:06 AM

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

All times are GMT -5. The time now is 07:41 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