JavaEar 专注于收集分享传播有价值的技术资料

Definition of 'entry' must be imported from module 'Darwin.POSIX.search' before it is required

#include <Foundation/Foundation.h>

int lookup (const struct entry dictionary[],const char search[], const int entries);

struct entry
{ 
    char word[15];
    char definition[50];
};


struct entry dictionary[100] = 
  {
    { "aardvark", "a burrowing African mammal" },
    { "abyss", "a bottomless pit" },
    { "acumen", "mentally sharp; keen" },
    { "addle", "to become confused" },
    { "aerie", "a high nest" },
    { "affix", "to append; attach" },
    { "agar", "a jelly made from seaweed" },
    { "ahoy", "a nautical call of greeting" },
    { "aigrette", "an ornamental cluster of feathers" },
    { "ajar", "partially opened" } 
  };

int lookup (const struct entry dictionary[],const char search[],const int entries)
{
    int i;

    for ( i = 0; i < entries; ++i )
        if ( strcmp(search, dictionary[i].word) == 0 )
            return i;
    return -1;
}


int main (void)
{   
    char word[10];

    int entries = 10;
    int entry;
    printf ("Enter word: ");
    scanf ("%14s", &word);
    entry = lookup (dictionary, word, entries);
    if ( entry != -1 )

        printf ("%s\n", dictionary[entry].definition);
    else
        printf ("The word %s is not in my dictionary.\n", word);
    return 0;
}

enter image description here

2个回答

    最佳答案
  1. Your code is wrong for so many reasons.

    To start with, with an array defined like

      char word[10];
    

    using

      scanf ("%14s", &word);
    

    is very wrong.

    • You don't need to pass the address of array variable, the array name decays to the pointer to the first element. with scanf(), %s expects an argument as a pointer to the start of a character array long enough to hold the converted input and the null terminator.

    • for an array size 10, which can hold a string of size 9 (plus, null-terminator), you're allowing 14 characters to be scanned and stored, this is invalid memory access which leads to undefined behavior.

    That said, you did not check for the success of the scanf() call, either. In case scanf() fails, you'll be accessing indeterminate values.

  2. 参考答案2
  3. You should do the declaration of the struct type struct entry before you use it the first time, which is in the prototype just before your declaration.