anything wrong with this code?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jim

    #1

    anything wrong with this code?

    try to run the following code and it cause core dump.

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

    int search (char *p[],char *name);

    char *names[]={
    "Herb",
    "Rex",
    "Daenns",
    "Joph",
    NULL};

    int main(void)
    {
    if (search(names," Daennis") !=-1)
    printf("Dennis is in list");
    return 0;
    }

    int search (char *p[],char * name)
    {
    while(*p++)
    {
    if (!strcmp(*p ,name)) return 1;
    } return -1;
    }

    But if I change the subroute to
    int search (char *p[],char * name)
    {
    while(*p)
    {
    if (!strcmp(*p ,name)) return 1;
    p++;
    } return -1;
    }

    It works, why while(*p++) cause core dump?
  • Mark A. Odell

    #2
    Re: anything wrong with this code?

    jim_lee2000@hot mail.com (jim) wrote in
    news:d94c72f0.0 408251043.26d0f 524@posting.goo gle.com:
    [color=blue]
    > try to run the following code and it cause core dump.[/color]
    [color=blue]
    > int search (char *p[],char * name)
    > {
    > while(*p++)
    > {
    > if (!strcmp(*p ,name)) return 1;
    > } return -1;
    > }
    >
    > But if I change the subroute to
    > int search (char *p[],char * name)
    > {
    > while(*p)
    > {
    > if (!strcmp(*p ,name)) return 1;
    > p++;
    > } return -1;
    > }
    >
    > It works, why while(*p++) cause core dump?[/color]

    Because you have an error. The first version says give mey the value at *p
    and be sure it is not NULL. Then increment to the next position (which may
    well be NULL) and use it! Bad programmer. The second case is correct and
    absolutely not equal to the first. Also I think you should drop the []
    from the function parameter since it is not technically correct. You are
    passing in two strings to be compared. So either use char *p (preferred)
    or char p[]. Of course p[] will be treated like char *p within search()
    anyhow (e.g. you cannot use sizeof p within search() to get the size of
    the array)

    --
    - Mark ->
    --

    Comment

    • Eric Sosman

      #3
      Re: anything wrong with this code?

      jim wrote:[color=blue]
      > try to run the following code and it cause core dump.
      > [...]
      > int search (char *p[],char * name)
      > {
      > while(*p++)
      > {
      > if (!strcmp(*p ,name)) return 1;
      > } return -1;
      > }
      >
      > But if I change the subroute to
      > int search (char *p[],char * name)
      > {
      > while(*p)
      > {
      > if (!strcmp(*p ,name)) return 1;
      > p++;
      > } return -1;
      > }
      >
      > It works, why while(*p++) cause core dump?[/color]

      The problem isn't with the `while', but with the
      position of the `++' relative to the strcmp() call.
      The second loop tests a `*p' value, and if it is non-
      NULL passes it to strcmp() and avances `p' to the next
      position. The first loop tests a `*p' value, advances
      `p' to the next position, and then passes the value from
      that un-tested position to strcmp().

      Second loop:

      while (no anthrax in next room)
      enter next room;

      First loop:

      while (no anthrax in *this* room)
      enter next room;

      See the difference?

      --
      Eric.Sosman@sun .com

      Comment

      • Mark A. Odell

        #4
        Re: anything wrong with this code?

        Eric Sosman <Eric.Sosman@su n.com> wrote in news:412CE54B.5 050007@sun.com:
        [color=blue]
        > Second loop:
        >
        > while (no anthrax in next room)
        > enter next room;
        >
        > First loop:
        >
        > while (no anthrax in *this* room)
        > enter next room;
        >
        > See the difference?[/color]

        Well put!

        --
        - Mark ->
        --

        Comment

        • Old Wolf

          #5
          Re: anything wrong with this code?

          "Mark A. Odell" <odellmark@hotm ail.com> wrote:[color=blue]
          > jim_lee2000@hot mail.com (jim) wrote
          >[color=green]
          > > try to run the following code and it cause core dump.[/color]
          >[color=green]
          > > int search (char *p[],char * name)
          > > {
          > > while(*p++)
          > > {
          > > if (!strcmp(*p ,name)) return 1;
          > > } return -1;
          > > }
          > >
          > > But if I change the subroute to
          > > int search (char *p[],char * name)
          > > {
          > > while(*p)
          > > {
          > > if (!strcmp(*p ,name)) return 1;
          > > p++;
          > > } return -1;
          > > }
          > >
          > > It works, why while(*p++) cause core dump?[/color]
          >
          > Because you have an error. The first version says give mey the value at *p
          > and be sure it is not NULL. Then increment to the next position (which may
          > well be NULL) and use it! Bad programmer. The second case is correct and
          > absolutely not equal to the first.[/color]

          It seems to be a common newbie mistake, to think that in:
          while (foo++) { ... }
          the ++ does not apply until the end of the loop iteration. (Same
          goes for if(foo++)).
          [color=blue]
          > Also I think you should drop the []
          > from the function parameter since it is not technically correct. You are
          > passing in two strings to be compared. So either use char *p (preferred)
          > or char p[].[/color]

          That would not have compiled correctly. He is passing in
          a list of strings, and a string to compare to each member
          of that list. Hence char *p[] (equivalent to: char **p) is
          indicated.

          Comment

          • Mark A. Odell

            #6
            Re: anything wrong with this code?

            oldwolf@inspire .net.nz (Old Wolf) wrote in
            news:843a4f78.0 408251738.65266 4f9@posting.goo gle.com:
            [color=blue][color=green]
            >> Also I think you should drop the []
            >> from the function parameter since it is not technically correct. You
            >> are passing in two strings to be compared. So either use char *p
            >> (preferred) or char p[].[/color]
            >
            > That would not have compiled correctly. He is passing in
            > a list of strings, and a string to compare to each member
            > of that list. Hence char *p[] (equivalent to: char **p) is
            > indicated.[/color]

            Good point. I missed that.



            --
            - Mark ->
            --

            Comment

            Working...