LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 05-06-2005, 12:48 AM   #1
lazyuser
LQ Newbie
 
Registered: May 2004
Location: US
Distribution: Debian - Sarge
Posts: 15

Rep: Reputation: 0
Command Line agruments


Hello,

I'm trying to familiarize myself with argc, and argv, but I am misisng someting and any help would be appericated.

My code is as follows:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

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

if (*argv[1] == 'h')
{
printf("help\n");
}

return 0;

}

WHen I run the program, it seg faults, but if I run it with the command line agrument h, it prints the message. So what I want to do is:

./1

return nothing

./1 h

print help

Any help would be great.

Lazyuser
 
Old 05-06-2005, 01:21 AM   #2
gobi_dgm
Member
 
Registered: Feb 2005
Location: Bangalore
Distribution: FC3
Posts: 137

Rep: Reputation: 15
It solves ur problem. The errors occured bcoz, u r trying to get the 1st element in char array, when it is empty.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc,char *argv[])
{
printf("%d:",argc);
if(argc>1)
{
if (*argv[1] == 'h')
{
printf("help\n");
}
return 0;
}
}

Pl update your profile..
 
Old 05-06-2005, 01:34 AM   #3
lazyuser
LQ Newbie
 
Registered: May 2004
Location: US
Distribution: Debian - Sarge
Posts: 15

Original Poster
Rep: Reputation: 0
Thanks,

That was exactly what I was looking for, also updated my profile, but not much to put in there.

Thanks again,

Lazyuser
 
  


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
Is there a single command to list all hardware installed (command line)? davee Linux - Hardware 6 02-28-2009 07:19 PM
Check agruments passing in C++, TruongAn Programming 1 06-17-2005 04:06 AM
Command to output file content line by line aznluvsmc Programming 2 09-12-2004 07:45 PM
51 characters only in the 1st Line of command line eggCover Linux - General 2 07-29-2004 01:28 PM
Where is Command line utility for Cups and command tutorial mossy Linux - Software 8 01-16-2004 12:24 AM

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

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