anything wrong with this code?

J

jim

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?
 
M

Mark A. Odell

(e-mail address removed) (jim) wrote in

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?

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)
 
E

Eric Sosman

jim said:
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?

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?
 
M

Mark A. Odell

Eric Sosman said:
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?

Well put!
 
O

Old Wolf

Mark A. Odell said:
(e-mail address removed) (jim) wrote
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?

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.

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++)).
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[].

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.
 
M

Mark A. Odell

(e-mail address removed) (Old Wolf) wrote in

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[].

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.

Good point. I missed that.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top