Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #98817 > unrolled thread
| Started by | Dexterr <dejangru@gmail.com> |
|---|---|
| First post | 2016-12-11 07:56 -0800 |
| Last post | 2016-12-12 14:18 -0800 |
| Articles | 20 on this page of 60 — 10 participants |
Back to article view | Back to comp.lang.c
[command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 07:56 -0800
Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 16:47 +0000
Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 09:16 -0800
Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 09:26 -0800
Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 19:05 +0000
Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 11:15 -0800
Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 11:53 -0800
Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 20:03 +0000
Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 12:26 -0800
Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 12:33 -0800
Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 20:33 +0000
Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 12:35 -0800
Re: [command line arguments] more elegant solution ?? "James R. Kuyper" <jameskuyper@verizon.net> - 2016-12-11 16:06 -0500
Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 13:11 -0800
Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 13:20 -0800
Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 13:25 -0800
Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 21:32 +0000
Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 13:44 -0800
Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 13:47 -0800
Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 21:56 +0000
Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 14:04 -0800
Re: [command line arguments] more elegant solution ?? Robert Spanjaard <spamtrap@arumes.com> - 2016-12-11 23:07 +0100
Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 14:15 -0800
Re: [command line arguments] more elegant solution ?? Robert Spanjaard <spamtrap@arumes.com> - 2016-12-11 23:32 +0100
Re: [command line arguments] more elegant solution ?? jameskuyper@verizon.net - 2016-12-11 14:42 -0800
Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 14:52 -0800
Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 14:40 -0800
Re: [command line arguments] more elegant solution ?? jameskuyper@verizon.net - 2016-12-11 14:55 -0800
Re: [command line arguments] more elegant solution ?? "James R. Kuyper" <jameskuyper@verizon.net> - 2016-12-11 19:44 -0500
Re: [command line arguments] more elegant solution ?? Richard Heathfield <rjh@cpax.org.uk> - 2016-12-11 23:20 +0000
Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 15:28 -0800
Re: [command line arguments] more elegant solution ?? jameskuyper@verizon.net - 2016-12-11 16:28 -0800
Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 18:57 +0000
Re: [command line arguments] more elegant solution ?? Barry Schwarz <schwarzb@dqel.com> - 2016-12-11 10:04 -0800
Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 11:25 -0800
Re: [command line arguments] more elegant solution ?? BartC <bc@freeuk.com> - 2016-12-11 18:29 +0000
Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 11:25 -0800
Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 20:30 +0000
Re: [command line arguments] more elegant solution ?? BartC <bc@freeuk.com> - 2016-12-11 23:43 +0000
Re: [command line arguments] more elegant solution ?? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2016-12-11 19:02 -0500
Re: [command line arguments] more elegant solution ?? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2016-12-11 19:13 -0500
Re: [command line arguments] more elegant solution ?? BartC <bc@freeuk.com> - 2016-12-12 00:55 +0000
Re: [command line arguments] more elegant solution ?? BartC <bc@freeuk.com> - 2016-12-12 01:02 +0000
Re: [command line arguments] more elegant solution ?? Keith Thompson <kst-u@mib.org> - 2016-12-11 19:02 -0800
Re: [command line arguments] more elegant solution ?? BartC <bc@freeuk.com> - 2016-12-12 11:50 +0000
Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-12 13:46 +0000
Re: [command line arguments] more elegant solution ?? BartC <bc@freeuk.com> - 2016-12-12 14:43 +0000
Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-12 22:09 +0000
Re: [command line arguments] more elegant solution ?? Keith Thompson <kst-u@mib.org> - 2016-12-12 08:58 -0800
Re: [command line arguments] more elegant solution ?? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2016-12-11 20:06 -0500
Re: [command line arguments] more elegant solution ?? jameskuyper@verizon.net - 2016-12-11 17:37 -0800
Re: [command line arguments] more elegant solution ?? Keith Thompson <kst-u@mib.org> - 2016-12-11 18:59 -0800
Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-12 02:47 +0000
Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 15:11 -0800
Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-12 00:25 -0800
Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-12 00:27 -0800
Re: [command line arguments] more elegant solution ?? Barry Schwarz <schwarzb@dqel.com> - 2016-12-12 01:01 -0800
Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-12 09:42 -0800
Re: [command line arguments] more elegant solution ?? "James R. Kuyper" <jameskuyper@verizon.net> - 2016-12-12 13:58 -0500
Re: [command line arguments] more elegant solution ?? Barry Schwarz <schwarzb@dqel.com> - 2016-12-12 14:18 -0800
Page 1 of 3 [1] 2 3 Next page →
| From | Dexterr <dejangru@gmail.com> |
|---|---|
| Date | 2016-12-11 07:56 -0800 |
| Subject | [command line arguments] more elegant solution ?? |
| Message-ID | <f5132d7e-048b-4115-ad9e-4f5cfa8533e7@googlegroups.com> |
Hi there. I have some questions about C programming concepts and solutions to ask you.
Yes, I am doing exercise for one very important exam in C programming. It is very important for
me to pass this exam. Thats true. My English is poor to describe how important that is.
I desperately need a help to finish my example.
- NO I don’t want and I DON’t expect that someone do it for me.
- I will do it, but I need some help in sense of directions how to solve some problems
- when I say “directions”, I mean I have some ideas and solutions but I am not sure are they good
- sometimes I find solutions but I don’t understand 100% why it works
- my exam example is cool and I like it and I love it and I am happy to work on it
On the other side, I need and I want and I will understand some concepts in C programming
and how it works. I am not satisfy to pass exam without understanding some issues. Yes, I have
a lot of books, video tutorials and so on, but I need some live person with C programming experience
to ask some questions and I will appreciate it a lot. On the other side, some newbies they read this will also learn something I hope so.
———
- > This is one small par of my exam exercise. I need to write a code to start a program with
three arguments from a command line. This arguments must be
< (1)name of the program> <(2)name of the input file> <(3) some number, some integer>
(1) - the name of the program is [./test]
(2) - input file is netlist file [test.netlist]
(3) - integer number is some number for omega [123]
The task is to print out “error” if someone type number of arguments != 3
or if he or she type the wrong name of the input file i.e. [my.netlist] instead [test.netlist]
- My questions:
1. Do you find some more elegant solution then I did it ?
2. If you ignore “if - condition” in main file and you type just [./test] the compiler
return “Segmentation fault: 11”
In the function “check input arguments” with this two “if condition” the compiler
return “segmentation fault” if you type only - ./test
that is the reason why I put in the main function one more “if condition” and now it
works but I am not sure is it good solution.
*** If the user type a wrong arguments or more or less arguments the program must
- return 1
- and print out - [ERR] usage: /test.netlist omega
What are you suggestions ?
Thanks a lot guys. ;)
———
- >> This is my solution:
------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int checkInputArguments(char *argument[], int arg_counter);
const char *str = “test.netlist";
i
int main(int argc, char *argv[])
{
if(argc == 1)
{
printf("ERR] usage: /test.netlist omega\\n\n");
return 1;
}
checkInputArguments(argv, argc);
return 0;
}
int checkInputArguments(char *argument[], int arg_counter)
{
if(strcmp(str, argument[1]) != 0)
{
printf("[ERR] usage: /test.netlist omega\\n\n");
return 1;
}
else if(arg_counter != 3)
{
printf("[ERR] usage: /test.netlist omega\\n\n");
return 1;
}
return 0;
}
[toc] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2016-12-11 16:47 +0000 |
| Message-ID | <87eg1exvi9.fsf@bsb.me.uk> |
| In reply to | #98817 |
Dexterr <dejangru@gmail.com> writes:
> Hi there. I have some questions about C programming concepts and
> solutions to ask you.
> - > This is one small par of my exam exercise. I need to write a code
> to start a program with three arguments from a command line. This
> arguments must be
>
> < (1)name of the program> <(2)name of the input file> <(3) some
> number, some integer>
>
>
> (1) - the name of the program is [./test]
>
> (2) - input file is netlist file [test.netlist]
>
> (3) - integer number is some number for omega [123]
>
> The task is to print out “error” if someone type number of arguments
> != 3 or if he or she type the wrong name of the input file
> i.e. [my.netlist] instead [test.netlist]
How odd. If the user has to supply that name and no other why allow it
to be specified at all? Are you sure you've understood the question?
> - My questions:
>
> 1. Do you find some more elegant solution then I did it ?
I've made some suggestions below your code.
> 2. If you ignore “if - condition” in main file and you type just
> [./test] the compiler
> return “Segmentation fault: 11”
It's not the compiler, it's the operating system being helpful. The
compiler translates your C into machine instructions, but when they are
run the operating system detects you are accessing something that does
not exit (the argv[1] in this case).
> In the function “check input arguments” with this two “if
> condition” the compiler return “segmentation fault” if you type
> only - ./test that is the reason why I put in the main function
> one more “if condition” and now it works but I am not sure is it
> good solution.
It's not. You have a checking function and really it should do all the
checking.
> *** If the user type a wrong arguments or more or less arguments
> the program must
> - return 1
You've not done this bit. The main function returns 0 in some cases
where you want it to return 1. If you fix the checking function to do
all the checks, there will be a simple way to decide if main should
return 0 or return 1.
(By the way, #include <stdlib.h> defines two macros EXIT_SUCCESS and
EXIT_FAILURE that are more portable than writing 0 and 1.)
> - and print out - [ERR] usage: /test.netlist omega
That's an odd message. For one thing it specifies the wrong string (you
said it should be "test.netlist" not "/test.netlist"), and it's more
usual to include the program name in a usage message.
<snip>
> #include <stdio.h>
> #include <string.h>
> #include <stdlib.h>
>
> int checkInputArguments(char *argument[], int arg_counter);
>
> const char *str = “test.netlist";
> i
Where does that 'i' come from? And that's not a valid string literal.
> int main(int argc, char *argv[])
> {
> if(argc == 1)
> {
> printf("ERR] usage: /test.netlist omega\\n\n");
> return 1;
> }
>
> checkInputArguments(argv, argc);
>
> return 0;
> }
>
> int checkInputArguments(char *argument[], int arg_counter)
> {
> if(strcmp(str, argument[1]) != 0)
> {
> printf("[ERR] usage: /test.netlist omega\\n\n");
> return 1;
> }
> else if(arg_counter != 3)
> {
> printf("[ERR] usage: /test.netlist omega\\n\n");
> return 1;
> }
> return 0;
> }
I would:
(a) put all the checking into the checkInputArguments function;
(b) use its return value in main to decide what exit status the program has;
(c) put the error message in only one place rather than three;
(d) print the error message to stderr (using fprintf);
(e) print the message that's actually specified (there's no \n in the
message you give in the text.
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Dexterr <dejangru@gmail.com> |
|---|---|
| Date | 2016-12-11 09:16 -0800 |
| Message-ID | <6c1666e7-6408-4700-a496-fc023c8c08f7@googlegroups.com> |
| In reply to | #98834 |
> (a) put all the checking into the checkInputArguments function;
Thanks Mr Ben on your response. I will do more changes. This is just first try or
first step to do all changes you suggest.
So, this part should go to this direction:
int checkInputArguments(char *argument[], int arg_counter)
{
if(argc == 1)
{
printf("[ERR] usage: /ass.netlist omega\\n\n");
return 1;
}
if(strcmp(str, argument[1]) != 0)
{
printf("[ERR] usage: /ass.netlist omega\\n\n");
return 1;
}
else if(arg_counter != 3)
{
printf("[ERR] usage: /ass.netlist omega\\n\n");
return 1;
}
return 0;
}
> (b) use its return value in main to decide what exit status the program has;
I don't understand this part.
>(c) put the error message in only one place rather than three;
If I have a "if condition" I only know to do on this way.
[toc] | [prev] | [next] | [standalone]
| From | Dexterr <dejangru@gmail.com> |
|---|---|
| Date | 2016-12-11 09:26 -0800 |
| Message-ID | <df433c63-a960-4cbd-9a27-1554fd5378e7@googlegroups.com> |
| In reply to | #98840 |
On Sunday, December 11, 2016 at 6:16:51 PM UTC+1, Dexterr wrote:
> > (a) put all the checking into the checkInputArguments function;
>
> Thanks Mr Ben on your response. I will do more changes. This is just first try or
> first step to do all changes you suggest.
> So, this part should go to this direction:
>
> int checkInputArguments(char *argument[], int arg_counter)
> {
> if(argc == 1)
> {
> printf("[ERR] usage: /ass.netlist omega\\n\n");
> return 1;
> }
>
> if(strcmp(str, argument[1]) != 0)
> {
> printf("[ERR] usage: /ass.netlist omega\\n\n");
> return 1;
> }
> else if(arg_counter != 3)
> {
> printf("[ERR] usage: /ass.netlist omega\\n\n");
> return 1;
> }
> return 0;
> }
>
> > (b) use its return value in main to decide what exit status the program has;
> I don't understand this part.
>
> >(c) put the error message in only one place rather than three;
> If I have a "if condition" I only know to do on this way.
----
- > Yes, the problem is, when I put in the same function from main, I got an "error message"
"error: use of undeclared identifier 'argc'"
if(argc == 1)
if(argc == 1)
> {
> printf("[ERR] usage: /ass.netlist omega\\n\n");
> return 1;
> }
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2016-12-11 19:05 +0000 |
| Message-ID | <87k2b6wal9.fsf@bsb.me.uk> |
| In reply to | #98843 |
Dexterr <dejangru@gmail.com> writes:
> On Sunday, December 11, 2016 at 6:16:51 PM UTC+1, Dexterr wrote:
>> > (a) put all the checking into the checkInputArguments function;
>>
>> Thanks Mr Ben on your response. I will do more changes. This is just first try or
>> first step to do all changes you suggest.
>> So, this part should go to this direction:
>>
>> int checkInputArguments(char *argument[], int arg_counter)
>> {
>> if(argc == 1)
>> {
>> printf("[ERR] usage: /ass.netlist omega\\n\n");
>> return 1;
>> }
>>
>> if(strcmp(str, argument[1]) != 0)
>> {
>> printf("[ERR] usage: /ass.netlist omega\\n\n");
>> return 1;
>> }
>> else if(arg_counter != 3)
>> {
>> printf("[ERR] usage: /ass.netlist omega\\n\n");
>> return 1;
>> }
>> return 0;
>> }
>>
>> > (b) use its return value in main to decide what exit status the program has;
>> I don't understand this part.
>>
>> >(c) put the error message in only one place rather than three;
>> If I have a "if condition" I only know to do on this way.
>
> ----
>
> - > Yes, the problem is, when I put in the same function from main, I got an "error message"
> "error: use of undeclared identifier 'argc'"
> if(argc == 1)
Yes, I missed that in my reply. Apart from variables you declare inside
a function (none in this case), a function can only refer to it's
parameters (in this case 'argument' and 'arg_counter') and to previously
declared file scope objects (which many people call global variables).
Your program has one of these called 'str' and the function does indeed
refer to it.
But the name 'argc' is in neither of these categories. In fact, you
pass the value of argc (a parameter of main) to the function precisely
so you can use that value inside the function -- but you must use the
parameter name you chose for it.
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Dexterr <dejangru@gmail.com> |
|---|---|
| Date | 2016-12-11 11:15 -0800 |
| Message-ID | <9d6038e2-b9f4-474b-8c7c-71bee08459ac@googlegroups.com> |
| In reply to | #98864 |
On Sunday, December 11, 2016 at 8:05:13 PM UTC+1, Ben Bacarisse wrote:
> Dexterr <dejangru@gmail.com> writes:
>
> > On Sunday, December 11, 2016 at 6:16:51 PM UTC+1, Dexterr wrote:
> >> > (a) put all the checking into the checkInputArguments function;
> >>
> >> Thanks Mr Ben on your response. I will do more changes. This is just first try or
> >> first step to do all changes you suggest.
> >> So, this part should go to this direction:
> >>
> >> int checkInputArguments(char *argument[], int arg_counter)
> >> {
> >> if(argc == 1)
> >> {
> >> printf("[ERR] usage: /ass.netlist omega\\n\n");
> >> return 1;
> >> }
> >>
> >> if(strcmp(str, argument[1]) != 0)
> >> {
> >> printf("[ERR] usage: /ass.netlist omega\\n\n");
> >> return 1;
> >> }
> >> else if(arg_counter != 3)
> >> {
> >> printf("[ERR] usage: /ass.netlist omega\\n\n");
> >> return 1;
> >> }
> >> return 0;
> >> }
> >>
> >> > (b) use its return value in main to decide what exit status the program has;
> >> I don't understand this part.
> >>
> >> >(c) put the error message in only one place rather than three;
> >> If I have a "if condition" I only know to do on this way.
> >
> > ----
> >
> > - > Yes, the problem is, when I put in the same function from main, I got an "error message"
> > "error: use of undeclared identifier 'argc'"
> > if(argc == 1)
>
> Yes, I missed that in my reply. Apart from variables you declare inside
> a function (none in this case), a function can only refer to it's
> parameters (in this case 'argument' and 'arg_counter') and to previously
> declared file scope objects (which many people call global variables).
> Your program has one of these called 'str' and the function does indeed
> refer to it.
>
> But the name 'argc' is in neither of these categories. In fact, you
> pass the value of argc (a parameter of main) to the function precisely
> so you can use that value inside the function -- but you must use the
> parameter name you chose for it.
>
> --
> Ben.
Thanks Mr Ben on your response. This is my solution.
- Your idea is genial to put return 1 in main function but
I don't know how to it but would be happy to put it as you suggest
All other aspect is now better, I think. Here it is:
--
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int checkInputArguments(char *argument[], int arg_counter);
void printErrorMessage();
const char *str = "netlist";
int main(int argc, char *argv[])
{
checkInputArguments(argv, argc);
return 0;
}
int checkInputArguments(char *argument[], int arg_counter)
{
if(arg_counter == 1)
{
printErrorMessage();
return 1;
}
else if(strcmp(str, argument[1]) != 0)
{
printErrorMessage();
return 1;
}
else if(arg_counter != 3)
{
printErrorMessage();
return 1;
}
// Checking does the 3rd argument is one integer or not
// else if(isdigit(**(argument + 2)) == 0)
else if(isdigit(*argument[2]) == 0)
{
printErrorMessage();
return 1;
}
return 0;
}
void printErrorMessage()
{
fprintf(stderr, "[ERR] usage: ./test netlist omega\n");
}
[toc] | [prev] | [next] | [standalone]
| From | Dexterr <dejangru@gmail.com> |
|---|---|
| Date | 2016-12-11 11:53 -0800 |
| Message-ID | <2be40ddd-6074-4db7-b2b3-ec40d33acf20@googlegroups.com> |
| In reply to | #98865 |
On Sunday, December 11, 2016 at 8:15:19 PM UTC+1, Dexterr wrote:
> On Sunday, December 11, 2016 at 8:05:13 PM UTC+1, Ben Bacarisse wrote:
> > Dexterr <dejangru@gmail.com> writes:
> >
> > > On Sunday, December 11, 2016 at 6:16:51 PM UTC+1, Dexterr wrote:
> > >> > (a) put all the checking into the checkInputArguments function;
> > >>
> > >> Thanks Mr Ben on your response. I will do more changes. This is just first try or
> > >> first step to do all changes you suggest.
> > >> So, this part should go to this direction:
> > >>
> > >> int checkInputArguments(char *argument[], int arg_counter)
> > >> {
> > >> if(argc == 1)
> > >> {
> > >> printf("[ERR] usage: /ass.netlist omega\\n\n");
> > >> return 1;
> > >> }
> > >>
> > >> if(strcmp(str, argument[1]) != 0)
> > >> {
> > >> printf("[ERR] usage: /ass.netlist omega\\n\n");
> > >> return 1;
> > >> }
> > >> else if(arg_counter != 3)
> > >> {
> > >> printf("[ERR] usage: /ass.netlist omega\\n\n");
> > >> return 1;
> > >> }
> > >> return 0;
> > >> }
> > >>
> > >> > (b) use its return value in main to decide what exit status the program has;
> > >> I don't understand this part.
> > >>
> > >> >(c) put the error message in only one place rather than three;
> > >> If I have a "if condition" I only know to do on this way.
> > >
> > > ----
> > >
> > > - > Yes, the problem is, when I put in the same function from main, I got an "error message"
> > > "error: use of undeclared identifier 'argc'"
> > > if(argc == 1)
> >
> > Yes, I missed that in my reply. Apart from variables you declare inside
> > a function (none in this case), a function can only refer to it's
> > parameters (in this case 'argument' and 'arg_counter') and to previously
> > declared file scope objects (which many people call global variables).
> > Your program has one of these called 'str' and the function does indeed
> > refer to it.
> >
> > But the name 'argc' is in neither of these categories. In fact, you
> > pass the value of argc (a parameter of main) to the function precisely
> > so you can use that value inside the function -- but you must use the
> > parameter name you chose for it.
> >
> > --
> > Ben.
>
> Thanks Mr Ben on your response. This is my solution.
> - Your idea is genial to put return 1 in main function but
> I don't know how to it but would be happy to put it as you suggest
>
> All other aspect is now better, I think. Here it is:
>
> --
>
> #include <stdio.h>
> #include <string.h>
> #include <stdlib.h>
> #include <ctype.h>
>
> int checkInputArguments(char *argument[], int arg_counter);
> void printErrorMessage();
>
> const char *str = "netlist";
>
>
> int main(int argc, char *argv[])
> {
>
> checkInputArguments(argv, argc);
> return 0;
> }
>
> int checkInputArguments(char *argument[], int arg_counter)
> {
>
> if(arg_counter == 1)
> {
> printErrorMessage();
> return 1;
> }
> else if(strcmp(str, argument[1]) != 0)
> {
> printErrorMessage();
> return 1;
> }
> else if(arg_counter != 3)
> {
> printErrorMessage();
> return 1;
> }
> // Checking does the 3rd argument is one integer or not
> // else if(isdigit(**(argument + 2)) == 0)
> else if(isdigit(*argument[2]) == 0)
> {
> printErrorMessage();
> return 1;
> }
> return 0;
> }
>
> void printErrorMessage()
> {
> fprintf(stderr, "[ERR] usage: ./test netlist omega\n");
> }
Sorry, I have found solution in main function for "return 1".
Here it is:
int main(int argc, char *argv[])
{
int check = checkInputArguments(argv, argc);
if(check == 0)
return 1;
return 0;
}
---
It works perfect. Thanks for this help. I have learn a lot on just this example.
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2016-12-11 20:03 +0000 |
| Message-ID | <877f76w7ve.fsf@bsb.me.uk> |
| In reply to | #98871 |
Dexterr <dejangru@gmail.com> writes:
<snip>
> Sorry, I have found solution in main function for "return 1".
> Here it is:
>
> int main(int argc, char *argv[])
> {
> int check = checkInputArguments(argv, argc);
> if(check == 0)
> return 1;
> return 0;
> }
I think your original specification had it the other way round (as I
would expect -- a zero exit code means "success"). checkInputArguments
returns 1 when it prints and error message which is when main should
return 1. It returns 0 when the arguments are all OK which is when main
should return 0. You might be albe to come up with a simpler way to
write that.
<snip>
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Dexterr <dejangru@gmail.com> |
|---|---|
| Date | 2016-12-11 12:26 -0800 |
| Message-ID | <f5d7f177-dd02-479d-b29c-6d24cdca6610@googlegroups.com> |
| In reply to | #98875 |
On Sunday, December 11, 2016 at 9:03:57 PM UTC+1, Ben Bacarisse wrote:
> Dexterr <dejangru@gmail.com> writes:
> <snip>
>
> I think your original specification had it the other way round (as I
> would expect -- a zero exit code means "success"). checkInputArguments
> returns 1 when it prints and error message which is when main should
> return 1. It returns 0 when the arguments are all OK which is when main
> should return 0. You might be albe to come up with a simpler way to
> write that.
>
> Yes! You have a right. I am an idiot, I am doing so fast and oversee some trivial things.
Here it is a solution.
int main(int argc, char *argv[])
{
int check = checkInputArguments(argv, argc);
if(check != 0)
return 1;
return 0;
}
--
That's is the most simpliest version that I can do it for now. I have no other idea.
> --
> Ben.
[toc] | [prev] | [next] | [standalone]
| From | Dexterr <dejangru@gmail.com> |
|---|---|
| Date | 2016-12-11 12:33 -0800 |
| Message-ID | <7b237a4d-c9a8-479c-bbc0-c7f106bbf541@googlegroups.com> |
| In reply to | #98878 |
On Sunday, December 11, 2016 at 9:27:08 PM UTC+1, Dexterr wrote:
> On Sunday, December 11, 2016 at 9:03:57 PM UTC+1, Ben Bacarisse wrote:
> > Dexterr <dejangru@gmail.com> writes:
> > <snip>
> >
>
> > I think your original specification had it the other way round (as I
> > would expect -- a zero exit code means "success"). checkInputArguments
> > returns 1 when it prints and error message which is when main should
> > return 1. It returns 0 when the arguments are all OK which is when main
> > should return 0. You might be albe to come up with a simpler way to
> > write that.
> >
> > Yes! You have a right. I am an idiot, I am doing so fast and oversee some trivial things.
>
> Here it is a solution.
>
> int main(int argc, char *argv[])
> {
> int check = checkInputArguments(argv, argc);
> if(check != 0)
> return 1;
> return 0;
> }
>
> --
> That's is the most simpliest version that I can do it for now. I have no other idea.
> > --
> > Ben.
OK. I think this is what you mean. ;)
int main(int argc, char *argv[])
{
if(checkInputArguments(argv, argc) != 0)
return 1;
return 0;
}
--
I like it very much. ;)
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2016-12-11 20:33 +0000 |
| Message-ID | <87vauqurx2.fsf@bsb.me.uk> |
| In reply to | #98878 |
Dexterr <dejangru@gmail.com> writes:
<snip>
> Here it is a solution.
>
> int main(int argc, char *argv[])
> {
> int check = checkInputArguments(argv, argc);
> if(check != 0)
> return 1;
> return 0;
> }
> That's is the most simpliest version that I can do it for now. I have
> no other idea.
If I told you that you would fail the exam if you declare a variable in
main, could you come up with another way to write this?
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Dexterr <dejangru@gmail.com> |
|---|---|
| Date | 2016-12-11 12:35 -0800 |
| Message-ID | <b142c0e4-966c-442a-be58-db679450e374@googlegroups.com> |
| In reply to | #98883 |
On Sunday, December 11, 2016 at 9:33:51 PM UTC+1, Ben Bacarisse wrote:
> Dexterr <dejangru@gmail.com> writes:
> <snip>
> > Here it is a solution.
> >
> > int main(int argc, char *argv[])
> > {
> > int check = checkInputArguments(argv, argc);
> > if(check != 0)
> > return 1;
> > return 0;
> > }
>
>
> > That's is the most simpliest version that I can do it for now. I have
> > no other idea.
>
> If I told you that you would fail the exam if you declare a variable in
> main, could you come up with another way to write this?
>
> --
> Ben.
Yes.
int main(int argc, char *argv[])
{
if(checkInputArguments(argv, argc) != 0)
return 1;
return 0;
}
[toc] | [prev] | [next] | [standalone]
| From | "James R. Kuyper" <jameskuyper@verizon.net> |
|---|---|
| Date | 2016-12-11 16:06 -0500 |
| Message-ID | <3c227a74-7bd1-431f-976d-ee426af5417e@verizon.net> |
| In reply to | #98885 |
On 12/11/2016 03:35 PM, Dexterr wrote:
> On Sunday, December 11, 2016 at 9:33:51 PM UTC+1, Ben Bacarisse wrote:
...
>> If I told you that you would fail the exam if you declare a variable in
>> main, could you come up with another way to write this?
>>
>> --
>> Ben.
>
> Yes.
>
> int main(int argc, char *argv[])
> {
> if(checkInputArguments(argv, argc) != 0)
> return 1;
> return 0;
> }
Hint: think about what values checkInputArguments() returns.
[toc] | [prev] | [next] | [standalone]
| From | Dexterr <dejangru@gmail.com> |
|---|---|
| Date | 2016-12-11 13:11 -0800 |
| Message-ID | <f17f6c9d-8745-46aa-942f-647ca067c886@googlegroups.com> |
| In reply to | #98898 |
On Sunday, December 11, 2016 at 10:06:47 PM UTC+1, James R. Kuyper wrote:
> On 12/11/2016 03:35 PM, Dexterr wrote:
> > On Sunday, December 11, 2016 at 9:33:51 PM UTC+1, Ben Bacarisse wrote:
> ...
> >> If I told you that you would fail the exam if you declare a variable in
> >> main, could you come up with another way to write this?
> >>
> >> --
> >> Ben.
> >
> > Yes.
> >
> > int main(int argc, char *argv[])
> > {
> > if(checkInputArguments(argv, argc) != 0)
> > return 1;
> > return 0;
> > }
>
> Hint: think about what values checkInputArguments() returns.
checkInputArguments() returns either 1 or 0. Zero for success and 1 for failure.
I have put !=0 because this is just a part of my exercise.
Inside of code it will be more return values for failure return 1, return 2, .... til return 5
[toc] | [prev] | [next] | [standalone]
| From | Dexterr <dejangru@gmail.com> |
|---|---|
| Date | 2016-12-11 13:20 -0800 |
| Message-ID | <0517cfc8-b167-45d3-b30a-a8ae2fc8f779@googlegroups.com> |
| In reply to | #98901 |
On Sunday, December 11, 2016 at 10:11:48 PM UTC+1, Dexterr wrote:
> On Sunday, December 11, 2016 at 10:06:47 PM UTC+1, James R. Kuyper wrote:
> > On 12/11/2016 03:35 PM, Dexterr wrote:
> > > On Sunday, December 11, 2016 at 9:33:51 PM UTC+1, Ben Bacarisse wrote:
> > ...
> > >> If I told you that you would fail the exam if you declare a variable in
> > >> main, could you come up with another way to write this?
> > >>
> > >> --
> > >> Ben.
> > >
> > > Yes.
> > >
> > > int main(int argc, char *argv[])
> > > {
> > > if(checkInputArguments(argv, argc) != 0)
> > > return 1;
> > > return 0;
> > > }
> >
> > Hint: think about what values checkInputArguments() returns.
>
> checkInputArguments() returns either 1 or 0. Zero for success and 1 for failure.
> I have put !=0 because this is just a part of my exercise.
> Inside of code it will be more return values for failure return 1, return 2, .... til return 5
OK, we can do it on this way.
int main(int argc, char *argv[])
{
// Checking input arguments
if(!checkInputArguments(argv, argc))
return 1;
return 0;
}
[toc] | [prev] | [next] | [standalone]
| From | Dexterr <dejangru@gmail.com> |
|---|---|
| Date | 2016-12-11 13:25 -0800 |
| Message-ID | <69fe960e-fe78-4857-af98-a24e3e8bf8ba@googlegroups.com> |
| In reply to | #98901 |
On Sunday, December 11, 2016 at 10:11:48 PM UTC+1, Dexterr wrote:
> On Sunday, December 11, 2016 at 10:06:47 PM UTC+1, James R. Kuyper wrote:
> > On 12/11/2016 03:35 PM, Dexterr wrote:
> > > On Sunday, December 11, 2016 at 9:33:51 PM UTC+1, Ben Bacarisse wrote:
> > ...
> > >> If I told you that you would fail the exam if you declare a variable in
> > >> main, could you come up with another way to write this?
> > >>
> > >> --
> > >> Ben.
> > >
> > > Yes.
> > >
> > > int main(int argc, char *argv[])
> > > {
> > > if(checkInputArguments(argv, argc) != 0)
> > > return 1;
> > > return 0;
> > > }
> >
> > Hint: think about what values checkInputArguments() returns.
>
> checkInputArguments() returns either 1 or 0. Zero for success and 1 for failure.
> I have put !=0 because this is just a part of my exercise.
> Inside of code it will be more return values for failure return 1, return 2, .... til return 5
OK, we can do it on this way.
int main(int argc, char *argv[])
{
// Checking input arguments
if(checkInputArguments(argv, argc))
return 1;
return 0;
}
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2016-12-11 21:32 +0000 |
| Message-ID | <87d1gyup6s.fsf@bsb.me.uk> |
| In reply to | #98885 |
Dexterr <dejangru@gmail.com> writes:
> On Sunday, December 11, 2016 at 9:33:51 PM UTC+1, Ben Bacarisse wrote:
>> Dexterr <dejangru@gmail.com> writes:
>> <snip>
>> > Here it is a solution.
>> >
>> > int main(int argc, char *argv[])
>> > {
>> > int check = checkInputArguments(argv, argc);
>> > if(check != 0)
>> > return 1;
>> > return 0;
>> > }
<snip>
>> If I told you that you would fail the exam if you declare a variable in
>> main, could you come up with another way to write this?
> Yes.
>
> int main(int argc, char *argv[])
> {
> if(checkInputArguments(argv, argc) != 0)
> return 1;
> return 0;
> }
Yes! An what if I said you've fail if main had more than one return
statement? (Don't worry if you can't see a way yet -- I'm just pushing
you for the sake of pushing.)
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Dexterr <dejangru@gmail.com> |
|---|---|
| Date | 2016-12-11 13:44 -0800 |
| Message-ID | <4e738c3b-7fcc-4fb3-9c09-64fea353d578@googlegroups.com> |
| In reply to | #98910 |
On Sunday, December 11, 2016 at 10:32:52 PM UTC+1, Ben Bacarisse wrote:
> Dexterr <dejangru@gmail.com> writes:
>
> > On Sunday, December 11, 2016 at 9:33:51 PM UTC+1, Ben Bacarisse wrote:
> >> Dexterr <dejangru@gmail.com> writes:
> >> <snip>
> >> > Here it is a solution.
> >> >
> >> > int main(int argc, char *argv[])
> >> > {
> >> > int check = checkInputArguments(argv, argc);
> >> > if(check != 0)
> >> > return 1;
> >> > return 0;
> >> > }
> <snip>
> >> If I told you that you would fail the exam if you declare a variable in
> >> main, could you come up with another way to write this?
>
> > Yes.
> >
> > int main(int argc, char *argv[])
> > {
> > if(checkInputArguments(argv, argc) != 0)
> > return 1;
> > return 0;
> > }
>
> Yes! An what if I said you've fail if main had more than one return
> statement? (Don't worry if you can't see a way yet -- I'm just pushing
> you for the sake of pushing.)
>
> --
> Ben.
That means, that I must write one more function inside of "if-statement in main" ???
[toc] | [prev] | [next] | [standalone]
| From | Dexterr <dejangru@gmail.com> |
|---|---|
| Date | 2016-12-11 13:47 -0800 |
| Message-ID | <10aee626-b7a9-42fa-9997-f46bdaa8ce95@googlegroups.com> |
| In reply to | #98912 |
On Sunday, December 11, 2016 at 10:44:22 PM UTC+1, Dexterr wrote:
> On Sunday, December 11, 2016 at 10:32:52 PM UTC+1, Ben Bacarisse wrote:
> > Dexterr <dejangru@gmail.com> writes:
> >
> > > On Sunday, December 11, 2016 at 9:33:51 PM UTC+1, Ben Bacarisse wrote:
> > >> Dexterr <dejangru@gmail.com> writes:
> > >> <snip>
> > >> > Here it is a solution.
> > >> >
> > >> > int main(int argc, char *argv[])
> > >> > {
> > >> > int check = checkInputArguments(argv, argc);
> > >> > if(check != 0)
> > >> > return 1;
> > >> > return 0;
> > >> > }
> > <snip>
> > >> If I told you that you would fail the exam if you declare a variable in
> > >> main, could you come up with another way to write this?
> >
> > > Yes.
> > >
> > > int main(int argc, char *argv[])
> > > {
> > > if(checkInputArguments(argv, argc) != 0)
> > > return 1;
> > > return 0;
> > > }
> >
> > Yes! An what if I said you've fail if main had more than one return
> > statement? (Don't worry if you can't see a way yet -- I'm just pushing
> > you for the sake of pushing.)
> >
> > --
> > Ben.
>
> That means, that I must write one more function inside of "if-statement in main" ???
int main(int argc, char *argv[])
{
// Checking input arguments
if(checkInputArguments(argv, argc))
errorFunction();
return 0;
}
int errorFunction()
{
return 1;
}
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2016-12-11 21:56 +0000 |
| Message-ID | <8737huuo2u.fsf@bsb.me.uk> |
| In reply to | #98913 |
Dexterr <dejangru@gmail.com> writes:
> On Sunday, December 11, 2016 at 10:44:22 PM UTC+1, Dexterr wrote:
>> On Sunday, December 11, 2016 at 10:32:52 PM UTC+1, Ben Bacarisse wrote:
>> > Dexterr <dejangru@gmail.com> writes:
<snip>
>> > > Yes.
>> > >
>> > > int main(int argc, char *argv[])
>> > > {
>> > > if(checkInputArguments(argv, argc) != 0)
>> > > return 1;
>> > > return 0;
>> > > }
>> >
>> > Yes! An what if I said you've fail if main had more than one return
>> > statement? (Don't worry if you can't see a way yet -- I'm just pushing
>> > you for the sake of pushing.)
>> >
>>
>> That means, that I must write one more function inside of
>> "if-statement in main" ???
>
> int main(int argc, char *argv[])
> {
> // Checking input arguments
> if(checkInputArguments(argv, argc))
> errorFunction();
> return 0;
> }
>
> int errorFunction()
> {
> return 1;
> }
That does not work. errorFunction returns 1 and execution continues in
main from the point of call. main then returns 0.
--
Ben.
[toc] | [prev] | [next] | [standalone]
Page 1 of 3 [1] 2 3 Next page →
Back to top | Article view | comp.lang.c
csiph-web