Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C函数中从数组返回项_C_Arrays_Pointers - Fatal编程技术网

在C函数中从数组返回项

在C函数中从数组返回项,c,arrays,pointers,C,Arrays,Pointers,所以,我在做这个刽子手游戏。我在数组和指针方面有点问题。这就是我所拥有的: char* getword(int index); int main (void) { char *secword = getword(1); printf("%s ", *secword); } char *getword(int index) { char *words[8]= {'bye', 'hi','what', 'cat', 'dog', 'bird', 'ai

所以,我在做这个刽子手游戏。我在数组和指针方面有点问题。这就是我所拥有的:

char* getword(int index);
int main (void) {

char *secword =  getword(1);
printf("%s ", *secword);
}

char *getword(int index)
{
 char *words[8]= {'bye', 'hi','what', 'cat', 'dog',
                    'bird', 'air', 'water', 'fire'};
return words[index];
}

我一直遇到一个分段错误(核心转储)错误。

您有四个重要错误

  • 您不能在
    getword()
    函数之外使用
    words
    ,因为它是在函数的堆栈框架中分配的

    因此,当函数返回时,数组被解除分配,从而发生未定义的行为

  • 您的数组不包含字符串,而是多字符contants。 多字符常量是有效的,但它们是实现定义的,因此您不能依赖它们来实现可移植程序

    您的代码之所以会编译,是因为您没有启用警告,然后将取决于多字符常量实现的整数值分配给数组的指针,当您尝试访问这些值处的地址以打印它们时,会发生未定义的行为

  • printf()
    要求每个
    “%s”
    说明符都有一个
    char
    指针,
    *secword
    具有类型
    char
    ,因此这也是错误的,因为您没有启用编译器警告,它正在重新编译

  • 您正在用
    9
    字初始化数组,而不是
    8
    ,这是编译器警告报告的另一个问题

  • 你有两个选择

  • getword()函数中将
    words
    设为静态

    const char *getword(int index);
    int main (void) 
    {
        char *secword =  getword(1);
        printf("%s\n", secword);
    }
    
    const char *getword(int index)
    {
         static const char *words[9] = {"bye",   "hi",  "what",  "cat", "dog",
                                        "bird", "air", "water", "fire"
         };
         return words[index];
    }
    
  • 在main中声明
    words
    ,并将其传递给
    getword()

  • 我写c程序已经有一段时间了,我尽可能多地使用编译器能给出的警告,如果我是学习c的新程序员,我会尽可能启用更多的警告

    char* getword(int index);
    
    int main (void) {  
          char *secword =  getword(1);
          printf("%s ", secword);
    }
    
    char *getword(int index)
    {
         static char *words[9]= {"bye", "hi","what", "cat", "dog",
                        "bird", "air", "water", "fire"};
        return words[index];
    }
    

    应该是这样的…

    您的程序有几个问题:

  • 使用
    char*
    变量(或任何指针)时,必须始终首先使用malloc分配内存。别忘了把它也放出来。手动分配内存的另一种方法是使用固定大小的数组

  • 打印字符串时不要取消对字符串的引用(
    printf(“%s”,*secword)

  • 确保在声明数组时指定了正确的元素数(有9个,而不是8个),并且不要忘记检查是否跨过数组边界

  • 在C语言中,所有字符串都是双引号;单引号表示字符常量


  • C中的字符串使用双引号。该数组中还有9个字符串,而不是8个。是的-您是如何做到segfault的??使用
    char const*
    而不是
    char*
    ,并且您需要使用
    printf(“%s”,secword))
    printf(“%c”,*secword)。注意编译消息;如果出现任何警告或错误,甚至不用麻烦运行程序--首先修复错误…假设返回本地数组指针的尝试没有首先失败,(假设任何东西都编译了).如果要从局部变量向函数返回值,请使用
    static
    限定符,因此在本例中,它应该是
    static char*words[8],因为当函数返回其堆栈帧时,其所有局部变量都将被销毁,但是如果你使用
    static
    你会把变量放在
    段中。data
    段istead of stackHave upvote-我想知道什么时候会有人最终建议const。static
    是不必要的,而
    words
    在返回后无效,
    words[index]
    是(字符串文字具有静态存储持续时间)。根据我的经验,注意警告比建议请求更多更重要——除了3,编译器已经发出警告(如果不是ANSI之前的编译器,我假设是这样)。
    char* getword(int index);
    
    int main (void) {  
          char *secword =  getword(1);
          printf("%s ", secword);
    }
    
    char *getword(int index)
    {
         static char *words[9]= {"bye", "hi","what", "cat", "dog",
                        "bird", "air", "water", "fire"};
        return words[index];
    }