0

This is a electronic dictionary program.But in this program, how can dict[i][0] compare to space.And it's going to be true.And how in the last if part dict[i][0] compare to space.Please anyone explain.

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


int main(void)
{
    char dict[][2][40] = {
        "house","a place of dwelling",
        "car","a vehicle",
        "computer","a thinking machine",
        "program","a sequence of instruction",
        "",""
    };

    char word[80];
    int i;

    printf("Enter word: ");
    gets(word);

    i = 0;

    while(strcmp(dict[i][0], "")){
        if(!strcmp(word, dict[i][0])){
            printf("Meaning: %s", dict[i][1]);
            break;
        }
        i++;
    }

    if(!strcmp(dict[i][0], ""))
        printf("Not in dictionary\n");

    return 0;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Kawsar_Ahmed
  • 73
  • 1
  • 1
  • 7
  • 1
    on line `"",""` (after `"program","a sequence of instruction"`) you set the end of the list; the strcmp is looking for that end marker so it knows you are at the end. you could write `"END"` instead or anything else, so long as you do the same thing in both places. – M.M Dec 08 '16 at 06:43

2 Answers2

0

In your code,

 strcmp(dict[i][0], "")

does not compare dict[i][0] against ) space, rather it's a check against empty string, which is the sentinel value of the defined array.

Also note, the type of dict[i][0] is char[40], which decays to a char *, so that's a valid argument for strcmp() anyway.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • but if i enter another word which is not in the list, that also print the "Not in dictionary" . is this for empty string.And how does it true in the while loop compare dict[i][0] to " " . is this means when dict[i][0] have nothing inside. – Kawsar_Ahmed Dec 08 '16 at 08:38
0

It's better to change the get(word); to scanf("%s", word); because it's deprecated and has buffer overflow problem. Also, the "" means empty string not space. Basically, the space is shown using one of ' ', '\r' or '\t' characters.

Back to your question, arrays in C are converted to pointers of that type, so dict[][2][40] is a 3D array but basically a 2D array of size 2x40 which means each entry of this array is a pointer of size 40.

So, you can rewrite your code using pointer arithmetic by putting the starting address of dict[0] in *j pointer and increasing it 40 by 40 for extracting the key of a specific value and 80 by 80 for reaching the next key.

char *j = dict[0];
while(strcmp(j, "")){
    if(!strcmp(word, j)){
        j = j + 40;
        printf("Meaning: %s\n", j);
        break;
    }
    j = j + 80;
}

if(!strcmp(j, ""))
    printf("Not in dictionary\n");
hmofrad
  • 1,784
  • 2
  • 22
  • 28
  • 2
    `scanf("%s", word)` is no safer than `gets(word)`. – Keith Thompson Dec 08 '16 at 07:21
  • Yes, You're right! There is a post here [link](http://stackoverflow.com/questions/3302255/c-scanf-vs-gets-vs-fgets) that says never use `gets()`, avoid using `scanf()`, but it is secure but annoying to use `fgets()`, since you need to determine the buffer size ahead of time. – hmofrad Dec 08 '16 at 15:39
  • `scanf()` *can* be used safely, but the `"%s"` format in particular is unsafe. You can use a count to limit the number of characters it will attempt to read: `"%80s"`. – Keith Thompson Dec 08 '16 at 16:16
  • I see! That's a simple yet effective workaround to limit the number of characters while using `scanf()`. – hmofrad Dec 08 '16 at 16:33