2017-09-05 56 views
-1
#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定义必须从模块“Darwin.POSIX.search”需要

+0

你如何得到引用的警告?用编译器编译它时?例如。 GCC? – Yunnosch

+0

欢迎来到SO。请在此处添加所有代码。欢迎链接到外部资源,但仅作为补充。也请更详细地描述您的问题并阅读:https://stackoverflow.com/help/mcve –

+0

停止发布非英文评论。堆栈溢出是一个英文网站。 –

回答

1

你的代码是错误的这么多的理由之前进口。

要使用

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

是非常错误的开头,以与阵列定义像

char word[10]; 

  • 你并不需要通过数组变量的地址,数组名衰变为指针的第一个元素。与scanf(),%s需要一个参数作为指向字符数组长度足以容纳转换输入和空终止符的指针。

  • 对于数组大小10,它可以容纳一个大小为9(加上,空终止符)的字符串,你允许扫描和存储14个字符,这是导致未定义行为的无效存储器访问。

这就是说,你没有检查的scanf()调用成功,无论是。如果scanf()失败,您将访问不确定的值。

+0

纠正我的代码请 –

+1

@АлександрБатин不能先生。我已经指出了错误,请展示一些努力,修复它,如果遇到问题,将很乐意提供帮助。 –

0

在第一次使用结构类型struct entry之前,您应该在声明之前的原型中进行声明。

相关问题