c question

J

jim

I am reading c:the complete reference, fourth edition by herbert
schildt.
I think the following program has a bug
#include <stdio.h>
#include <string.h>
#include <ctype.h>

char *dic[][40]={
"atlas","A volume of maps",
"car","A motorized vehicle",
"telephone","a communication device",
"airport","A flying matchine",
"",""
};

int main(void)
{
char word[80],ch;
char **p;

do{
puts("\nenter words:");
scanf("%s",word);
p=(char **)dic;
do {
if(!strcmp(*p,word)){
puts("meaning:");
puts(*(p+1));
break;
}
if(!strcmp(*p,word)) break;
p=p+2;
}while(*p);
if(!*p) puts("word not in dictionary");
printf("Another ?:");
scanf(" %c" , &ch);
} while(toupper(ch) !='N');
return 0;
}



Although the above version runs, I think the definition of dic is not
correct. the correct version should be
#include <stdio.h>
#include <string.h>
#include <ctype.h>

char dic[][40]={
"atlas","A volume of maps",
"car","A motorized vehicle",
"telephone","a communication device",
"airport","A flying matchine",
"",""
};

int main(void)
{
char word[80],ch;
char (*p)[40];

do{
puts("\nenter words:");
scanf("%s",word);
p=(char (*)[40])dic;
do {
if(!strcmp(*p,word)){
puts("meaning:");
puts(*(p+1));
break;
}
if(!strcmp(*p,word)) break;
p=p+2;
}while(**p);
if(!**p) puts("word not in dictionary");
printf("Another ?:");
scanf(" %c" , &ch);
} while(toupper(ch) !='N');
return 0;
}
 
A

A. Bolmarcich

I am reading c:the complete reference, fourth edition by herbert
schildt.
I think the following program has a bug [snip program]
Although the above version runs, I think the definition of dic is not
correct. the correct version should be
[snip program]

What Schildt wrote works because the "wrong" declaration of dic

char *dic[][40]

is compensated for by the cast in the assignment

p=(char **)dic;

In Schildt's version the array initializer makes the size of the []
dimension of dic 1 and sizeof(dic) == 40*sizeof(char *). Don't try
to use 40 initial values with Schildt's version.

What you wrote is more correct and you can remove the cast from the
assignment

p=(char (*)[40])dic

because in your version the type of the expression

dic

is char (*)[40]. In general, the few casts a program needs, the
better. In your version the array initializer makes the size of
the [] dimension of dic 10 and sizeof(dic) == 10*40.

Because the program never uses dic as a two-dimensional array, a
better version would be to take Schildt's version but define dic as

char *dic[]={
"atlas","A volume of maps",
"car","A motorized vehicle",
"telephone","a communication device",
"airport","A flying matchine",
0
};

and remove the cast from the line

p=(char **)dic;

In this version sizeof(dic) == 9 * sizeof(char *), where the
factor of 9 occurs because the array is initialized with 9 values.
 
E

E. Robert Tisdale

jim wrote:

[snip]
> cat main.c
#include <stdio.h>
#include <string.h>
#include <ctype.h>

const char *dic[] = {
"atlas", "A volume of maps",
"car", "A motorized vehicle",
"telephone", "A communication device",
"airplane", "A flying machine",
"", ""
};

int main(int argc, char* argv[]) {
char ch = 'Y';
do{
char word[80];
puts("\nenter words:");
scanf("%s", word);
const char** p = (const char **)dic;
do {
if(!strcmp(*p, word)){
puts("meaning:");
puts(*(p+1));
break;
}
p = p + 2;
} while(*p);
if(!*p) puts("word not in dictionary");
printf("Another?: ");
scanf(" %c", &ch);
} while('N' != toupper(ch));
return 0;
}
> gcc -Wall -std=c99 -pedantic -o main main.c
> ./main

enter words:
car
meaning:
A motorized vehicle
Another?: y

enter words:
atlas
meaning:
A volume of maps
Another?: n
 
F

Flash Gordon

On 26 Aug 2004 14:34:43 -0700
I am reading c:the complete reference, fourth edition by herbert
schildt.

That is your first problem. Do a search of this group to find all the
reasons why you should bin it and get a decent book instead.
I think the following program has a bug

Probably, it's by Schildt.
#include <stdio.h>
#include <string.h>
#include <ctype.h>

char *dic[][40]={
"atlas","A volume of maps",
"car","A motorized vehicle",
"telephone","a communication device",
"airport","A flying matchine",
"",""
};

This is obviously not what was wanted as you correctly spotted.
char *dic[]={
"atlas","A volume of maps",
"car","A motorized vehicle",
"telephone","a communication device",
"airport","A flying matchine",
NULL,NULL
};
int main(void)
{
char word[80],ch;
char **p;

do{
puts("\nenter words:");
scanf("%s",word);

Type in an 80 character line and this will overflow the buffer.
p=(char **)dic;

Get rid of the cast. All it does is hide the fact that dic was
incorrectly defined.

Casts in general are a bad thing. There are some situations where they
are required, but unless you understand exactly why a cast is required
don't use it.

To the pedants (like me) here, obviously in a situation where a cast is
required it is not a bad thing, hence my saying "in general" rather than
always above. :)
do {
if(!strcmp(*p,word)){
puts("meaning:");
puts(*(p+1));
break;
}
if(!strcmp(*p,word)) break;

The above line is obviously redundant and can be deleted.
p=p+2;
}while(*p);
if(!*p) puts("word not in dictionary");
printf("Another ?:");
scanf(" %c" , &ch);

This is just plain silly. Try entering no at the prompt then a word in
the dictionary at the next prompt.
} while(toupper(ch) !='N');
return 0;
}

Although the above version runs, I think the definition of dic is not
correct. the correct version should be

<snip>

Whilst your version had fixed one problem it suggested to me that you
have been mislead by Shildt. I've not fully corrected the code (at the
very least I have not fixed the scanf problems) but I think I have shown
enough to indicate why you should bin the book.

Also, if I was trying to write this program from scratch I would
have done things rather differently.
 
C

ChokSheak Lau

do you mind using a more meaningful subject title than "c question" on a
C newsgroups? thank you.

chok
 
D

Default User

ChokSheak said:
do you mind using a more meaningful subject title than "c question" on a
C newsgroups? thank you.


While we're on netiquette issues, would you mind not top-posting?




Brian Rodenborn
 

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

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top