Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: Why does it not work? (http://www.velocityreviews.com/forums/t962144-re-why-does-it-not-work.html)

Lew Pitcher 06-25-2013 09:43 PM

Re: Why does it not work?
 
On Tuesday 25 June 2013 17:08, in comp.lang.c, 2970invalid@webuse.net wrote:

> paskali <2970invalid@webuse.net> wrote:
>
> In the first case, if i leave empty command line arguments, it tells me:
>
> ERROR
> You wrote (null)


Your compiler was being nice, I see.
You invoked execution-time undefined behaviour, and got lucky.

> #include <stdio.h>
>
> int main(int argc, char *argv[]) {
>
> ********if(argv[1] == "test")
> ****************puts("OK!");
> ********else
> ****************puts("ERROR");
>
> ********printf("You wrote %s\n", argv[1]);
>
> ********return 0;
>
> }


If argc & argv are provided for by the compilation system, argv is required
to point to an array of pointers-to-char, and argc is required to hold a
non-negative count of the number of argv array elements.

If argc is greater than zero, then argv[0] is required to point to a string
that "represents the program name".

The remaining argv[] values (if any) are required to point
to "implementation-defined values" given "by the host environment priorto
program startup". argv[argc] is required to "be a null pointer".

You gave the command no arguments; in most environments, that would result
in
argc being set to 1
argv[0] being set to a pointer
that points to a string representing the name of the program,and
argv[1] being set to a null pointer (i.e. NULL)

Your logic does not test the argc count, and presumes that argv[1] points to
a character string. It does not; it points to NULL. Your compiler took pity
on your mistake, and substituted a pointer to the string "(null)" for the
NULL value, when you passed that value to the printf() statement.

> (it should be the same result in the 99.99% of the compilers).


I don't know where you get that figure from. I doubt the figure. I would
suspect that there are many compilers that would be happy to let you
segfault while trying to access unaccessable or protected memory at the
NULL address.

> In the second case, with an empty line the program crashes and it tells
> me the very nice phrase:
>
> Segmentation fault


Now, your compiler declined to take pity on your mistake. You again invoked
execution-time undefined behaviour, and got unlucky.

> #include <stdio.h>
> #include <string.h>
>
> int main(int argc, char *argv[]) {
>
> ********if(strcmp(argv[1], "test") == 0)
> ****************puts("OK!");
> ********else
> ****************puts("ERROR");
>
> ********printf("You wrote %s\n", argv[1]);
>
> ********return 0;
>
> }


With
argc being set to 1
argv[0] being set to a pointer
that points to a string representing the name of the program,and
argv[1] being set to a null pointer (i.e. NULL)

the strcmp() attempted to access unaccessable or protected memory at the
NULL address supplied by argv[1], and did the sensible thing: segfault.


In both cases, the code suffered at least one logic error, and failed.

HTH
--
Lew Pitcher
"In Skills, We Trust"


All times are GMT. The time now is 12:11 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.