c question

Discussion in 'C Programming' started by jim, Aug 26, 2004.

  1. jim

    jim Guest

    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;
    }
    jim, Aug 26, 2004
    #1
    1. Advertising

  2. jim

    CBFalconer Guest

    jim wrote:
    >
    > 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>


    Of course. It is by Schildt. Never expect correct code from him.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Aug 27, 2004
    #2
    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. sean
    Replies:
    1
    Views:
    589
    Cowboy \(Gregory A. Beamer\)
    Oct 20, 2003
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    644
    Iain Norman
    Feb 16, 2005
  3. =?Utf-8?B?YW5kcmV3MDA3?=

    question row filter (more of sql query question)

    =?Utf-8?B?YW5kcmV3MDA3?=, Oct 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    939
    Scott Allen
    Oct 6, 2005
  4. Philip Meyer
    Replies:
    0
    Views:
    413
    Philip Meyer
    Nov 30, 2003
  5. Bit Byte
    Replies:
    1
    Views:
    836
    Teemu Keiski
    Jan 28, 2007
Loading...

Share This Page