what am i doing wrong? 
Author Message
 what am i doing wrong?

heres my code (yes im a newbie)

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

int main (int argc, char **argv) {
int count;
int i,s;
char *p;

for (count = 0; count < argc; ++count) {

if (strncmp("-f", argv[count],sizeof("f")) == 0) {
        printf("you entered f!\n");

        for (p=argv[count];p!=NULL;p++){
                if (isdigit(p)) {
                    for(i=2;i<argc;i++)
                    s=s+atoi(argv[i]);
                    }
                }
        }

Quote:
}
return 0;
}

root:~/mysourcecode# ./a.out -f 34
you entered f!
Segmentation fault


Mon, 12 Sep 2005 22:08:22 GMT  
 what am i doing wrong?


Quote:
> heres my code (yes im a newbie)

So were we all, once.

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

> int main (int argc, char **argv) {

Ha! He calls himself a newbie! And yet he defines `main` correctly!

Quote:
> int count;
> int i,s;
> char *p;

(usually the nested stuff gets indented, as you do below)

Quote:
> for (count = 0; count < argc; ++count) {

> if (strncmp("-f", argv[count],sizeof("f")) == 0) {

Hmm. That looks ... strange. I would write it as

    if (strcmp( "-f", argv[count] ) == 0)

`strcmp` seems to ne the man for the job, unless you're trying
to catch -ff, -fred, -foo, -forgiveme etc as well?

[Actually I'd write it as

    ... strcmp( arg, "-f" ) ...

because I'd have defined char *arg = argv[count] and I prefer the
order that way round, but that's stylistic.]

Quote:
>    printf("you entered f!\n");

>    for (p=argv[count];p!=NULL;p++){

I don't think you meant `p != NULL`. I think you meant `*p != 0`.
You're advancing `p` ever onwards, past its defined boundary
(the end of the argument string), onwards and upwards and BOOM!
today. It's Undefined Behaviour and thus a Bad Thing.

Quote:
>            if (isdigit(p)) {

Oops. First, you forgot to #include <ctype.h> to declare `isdigit`;
the compiler will have declared it for you as a "function taking
something eg a pointer and returning int". I hope it complained that
you hadn't declared `isdigit`.

Second, the argument to `isdigit` must have the value of an unsigned
char or EOF. It is unlikely that a pointer, if it survives the
transformation to `int` forced on it by the call, will have such a value;
so you get Undefined Bahaviour again. *This* time, it manifests as
a segfault; probably `isdigit` is implemented using its argument as
an index into a small array. BOOM today.

Quote:
>                for(i=2;i<argc;i++)
>                s=s+atoi(argv[i]);

`s` is uninitialised. Getting its value is Undefined Bahaviour.
(And `atoi` is unsafe.)

Quote:
>                }
>            }
>    }
> }
> return 0;
> }

> root:~/mysourcecode# ./a.out -f 34
> you entered f!
> Segmentation fault

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html


Mon, 12 Sep 2005 22:44:11 GMT  
 what am i doing wrong?


Quote:

>>                if (isdigit(p)) {

> Oops. First, you forgot to #include <ctype.h> to declare `isdigit`;
> the compiler will have declared it for you as a "function taking
> something eg a pointer and returning int". I hope it complained that
> you hadn't declared `isdigit`.

> Second, the argument to `isdigit` must have the value of an unsigned
> char or EOF. It is unlikely that a pointer, if it survives the
> transformation to `int` forced on it by the call, will have such a value;
> so you get Undefined Bahaviour again. *This* time, it manifests as
> a segfault; probably `isdigit` is implemented using its argument as
> an index into a small array. BOOM today.

... and so you probably meant `isdigit(*p)` ...

--
Chris "proof-read BEFORE sending" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html



Mon, 12 Sep 2005 22:49:51 GMT  
 what am i doing wrong?

Quote:
>heres my code (yes im a newbie)

>char *p;
...
>            if (isdigit(p)) {

What is isdigit expecting?  What are you giving it instead?

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Mon, 12 Sep 2005 23:28:37 GMT  
 what am i doing wrong?

Quote:

> heres my code (yes im a newbie)

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

> int main (int argc, char **argv) {
> int count;
> int i,s;
> char *p;

> for (count = 0; count < argc; ++count) {

> if (strncmp("-f", argv[count],sizeof("f")) == 0) {

Why are you using sizeof("f")? It looks like you are looking at each
command line argument (including the program name) to see if it starts
with "-f". If that is what you want then you want to compare the first two
characters of argv[count] to "-f". The sizeof("f") happens to be 2 but
that is just because you have deleted one character from it. The following
is just as valid:

        strncmp("-f", argv[count], sizeof("q")) == 0) {

It would make more sense to use something like:

        char switch[] = "-f";

        strncmp(switch, argv[count], strlen(switch)) == 0) {

This way if you change the "-f" to something with a different length it
will automatically change the strncmp call.

Quote:
>    printf("you entered f!\n");

If you use the switch variable you can use it in the printf as well.

Quote:
>    for (p=argv[count];p!=NULL;p++){

I think the problem is the exit condition here. The variable p appears to
be walking through each argv[count] a character at a time. The value of *p
will equal '\0' when you reach the end of the string. I believe that is
what you want as your exit condition.

Quote:
>            if (isdigit(p)) {

The variable p is a pointer to car. The isdigit function requires a char
(actually it requires an int but I won't get into that). Now *p is a char.
So I think you want isdigit(*p).

Quote:
>                for(i=2;i<argc;i++)
>                s=s+atoi(argv[i]);
>                }
>            }
>    }
> }
> return 0;
> }

Generally speaking, it is not clear what you are trying to do. You might
want to put comments indicating what you think the code is doing.

Quote:
> root:~/mysourcecode# ./a.out -f 34
> you entered f!
> Segmentation fault

Pretty sure it is the p!=NULL but the other things should be cleaned up as
well.

--

"iqgbgxmdbjlgdv.lksrqek.n";char *strchr(const char *,int);while(
*i){j+=strchr(t,*i++)-t;j%=sizeof t-1;putchar(t[j]);} return 0;}



Tue, 13 Sep 2005 02:39:23 GMT  
 what am i doing wrong?

Quote:
}

> root:~/mysourcecode# ./a.out -f 34
^^^^^
> you entered f!
> Segmentation fault

Maybe you should reconsider using root account until you
get better "handle" on C. IMHO.


Tue, 13 Sep 2005 06:19:08 GMT  
 what am i doing wrong?
On Thu, 27 Mar 2003 22:19:08 GMT, in comp.lang.c , "nobody"

Quote:



>}

>> root:~/mysourcecode# ./a.out -f 34
>^^^^^
>> you entered f!
>> Segmentation fault

>Maybe you should reconsider using root account until you
>get better "handle" on C. IMHO.

That would be a default linux installation.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>



Tue, 13 Sep 2005 07:31:52 GMT  
 what am i doing wrong?

Quote:



>> heres my code (yes im a newbie)

> So were we all, once.

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

>> int main (int argc, char **argv) {

> Ha! He calls himself a newbie! And yet he defines `main` correctly!

I believe that's 3 in a row.  Someone call Charles Fort.

Quote:
>> int count;
>> int i,s;
>> char *p;

> (usually the nested stuff gets indented, as you do below)

>> for (count = 0; count < argc; ++count) {

>> if (strncmp("-f", argv[count],sizeof("f")) == 0) {

> Hmm. That looks ... strange. I would write it as

>    if (strcmp( "-f", argv[count] ) == 0)

> `strcmp` seems to ne the man for the job, unless you're trying
> to catch -ff, -fred, -foo, -forgiveme etc as well?

> [Actually I'd write it as

>    ... strcmp( arg, "-f" ) ...

> because I'd have defined char *arg = argv[count] and I prefer the
> order that way round, but that's stylistic.]

>>       printf("you entered f!\n");

>>       for (p=argv[count];p!=NULL;p++){

> I don't think you meant `p != NULL`. I think you meant `*p != 0`.
> You're advancing `p` ever onwards, past its defined boundary
> (the end of the argument string), onwards and upwards and BOOM!
> today. It's Undefined Behaviour and thus a Bad Thing.

>>               if (isdigit(p)) {

> Oops. First, you forgot to #include <ctype.h> to declare `isdigit`;
> the compiler will have declared it for you as a "function taking
> something eg a pointer and returning int". I hope it complained that
> you hadn't declared `isdigit`.

> Second, the argument to `isdigit` must have the value of an unsigned
> char or EOF. It is unlikely that a pointer, if it survives the
> transformation to `int` forced on it by the call, will have such a value;
> so you get Undefined Bahaviour again. *This* time, it manifests as
> a segfault; probably `isdigit` is implemented using its argument as
> an index into a small array. BOOM today.

>>                   for(i=2;i<argc;i++)
>>                   s=s+atoi(argv[i]);

> `s` is uninitialised. Getting its value is Undefined Bahaviour.
> (And `atoi` is unsafe.)

Sometimes atoi is OK, particularly when parsing commandline arguments;
if a 0 value is bad input for some parameter anyway, then you might as
well use atoi.

In this case, if the spec is "calculate the sum of the command line
arguments that can be converted to integers" then using atoi is OK
anyway, because the 0 result won't affect the sum.

        - Kevin.



Tue, 13 Sep 2005 10:32:18 GMT  
 what am i doing wrong?

Quote:

> On Thu, 27 Mar 2003 22:19:08 GMT, in comp.lang.c , "nobody"



>>}

>>> root:~/mysourcecode# ./a.out -f 34
>>^^^^^
>>> you entered f!
>>> Segmentation fault

>>Maybe you should reconsider using root account until you
>>get better "handle" on C. IMHO.

> That would be a default linux installation.

No, it wouldn't.  Though I've heard Lindows does this, all the
mainstream linux distributions I'm aware of create a user account during
installation and urge you to use it for day-to-day tasks.

         - Kevin.



Tue, 13 Sep 2005 11:03:13 GMT  
 what am i doing wrong?
On Fri, 28 Mar 2003 03:03:13 GMT, in comp.lang.c , Kevin Easton

Quote:


>> That would be a default linux installation.

>No, it wouldn't.  

My redhat install did this. A couple of years old tho.

Quote:
>Though I've heard Lindows does this, all the
>mainstream linux distributions I'm aware of create a user account during
>installation

Yes, NT/W2K do this too.  

Quote:
>and urge you to use it for day-to-day tasks.

its the "urge" part that gets to me. It ought to be *really* hard to
use the root login. Renaming it something complicated to type would
probably help....

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>



Wed, 14 Sep 2005 07:06:59 GMT  
 what am i doing wrong?


[snip]

Quote:
> > `s` is uninitialised. Getting its value is Undefined Bahaviour.
> > (And `atoi` is unsafe.)

> Sometimes atoi is OK, particularly when parsing commandline arguments;

Values which would overflow an int can be supplied
on command line.  So that's not a valid exception.

Quote:
> if a 0 value is bad input for some parameter anyway, then you might as
> well use atoi.

If you do, you're subject to possible undefined behavior due
to overflow.

Quote:

> In this case, if the spec is "calculate the sum of the command line
> arguments that can be converted to integers" then using atoi is OK
> anyway, because the 0 result won't affect the sum.

But values such as 83624175426 easily can, in a not
very nice way. :-)

-Mike



Wed, 14 Sep 2005 16:11:25 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. what am I doing wrong here?

2. What am I doing wrong?

3. What am I doing wrong?

4. WHAT AM I DOING WRONG?

5. what am i doing wrong!

6. What am I doing wrong (part 2)

7. What am i doing wrong?

8. What am i doing wrong in this code?

9. What am I doing wrong here

10. What am I doing Wrong?

11. what am i doing wrong

12. what am i doing wrong???

 

 
Powered by phpBB® Forum Software