Re: Why does it not work?

Discussion in 'C Programming' started by Lew Pitcher, Jun 25, 2013.

  1. Lew Pitcher

    Lew Pitcher Guest

    On Tuesday 25 June 2013 17:08, in comp.lang.c, wrote:

    > paskali <> 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"
     
    Lew Pitcher, Jun 25, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jblazi
    Replies:
    5
    Views:
    448
    jblazi
    Aug 16, 2004
  2. Horace Nunley

    why why why does function not work

    Horace Nunley, Sep 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    471
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Sep 27, 2006
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    922
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,077
    Smokey Grindel
    Dec 2, 2006
  5. Tarun
    Replies:
    5
    Views:
    407
    Tarun
    Jul 14, 2005
Loading...

Share This Page