C语言中字符串数组的奇怪行为

时间:2017-06-20 08:05:16

标签: c

我在使用C语言时遇到了一些困难 - 尤其是在文件I / O方面。我已经查看了一些以前的线程,看看如何在C中创建字符串数组,我已经想出了这个。

void CreateBiomes()
{
    const int STRING_LENGTH = 32;
    const int BIOME_COUNT = 63;
    const char *biomes[BIOME_COUNT][STRING_LENGTH+1] = {"beaches", "birch_forest", "birch_forest_hills", "cold_beach", "deep_ocean", "desert", "desert_hills", "extreme_hills", "extreme_hills_with_trees", "forest", "forest_hills", "frozen_ocean", "frozen_river", "hell", "ice_flats", "ice_mountains", "jungle", "jungle_edge", "jungle_hills", "mesa", "mesa_clear_rock", "mesa_rock", "mushroom_island", "mushroom_island_shore", "mutated_birch_forest", "mutated_birch_forest_hills", "mutated_desert", "mutated_extreme_hills", "mutated_extreme_hills_with_trees", "mutated_forest", "mutated_ice_flats", "mutated_jungle", "mutated_jungle_edge", "mutated_mesa", "mutated_mesa_clear_rock", "mutated_mesa_rock", "mutated_plains", "mutated_redwood_taiga", "mutated_redwood_taiga_hills", "mutated_roofed_forest", "mutated_savanna", "mutated_savanna_rock", "mutated_swampland", "mutated_taiga", "mutated_taiga_cold", "ocean", "plains", "redwood_taiga", "redwood_taiga_hills", "river", "roofed_forest", "savanna", "savanna_rock", "sky", "smaller_extreme_hills", "stone_beach", "swampland", "taiga", "taiga_cold", "taiga_cold_hills", "taiga_hills", "void"};

    for(int i = 0; i <= BIOME_COUNT; i++)
    {
        printf("%s\n", *biomes[i]);
    }

    return;
}

问题是 - 此代码仅适用于程序崩溃前的“海滩”和“mutated_mesa”。一切都编译得很好,它只是不会处理我在数组中列出的任何其他字符串,而是打印一个(null)。这是为什么?

3 个答案:

答案 0 :(得分:0)

尝试替换声明

const char *biomes[BIOME_COUNT][STRING_LENGTH+1]

如下。

const char biomes[BIOME_COUNT][STRING_LENGTH+1]

答案 1 :(得分:0)

你的数组是一个二维指针数组,并且你在printf循环中索引了太多。

我已将其更改为1-D指针数组,并且还删除了硬编码大小。相反,我用"void"指针替换了最后一个字符串NULL,并使用它来控制循环。

#include <stdio.h>

void CreateBiomes(void)
{
    const char *biomes[] = {"beaches", "birch_forest", "birch_forest_hills", "cold_beach", "deep_ocean", "desert", "desert_hills", "extreme_hills", "extreme_hills_with_trees", "forest", "forest_hills", "frozen_ocean", "frozen_river", "hell", "ice_flats", "ice_mountains", "jungle", "jungle_edge", "jungle_hills", "mesa", "mesa_clear_rock", "mesa_rock", "mushroom_island", "mushroom_island_shore", "mutated_birch_forest", "mutated_birch_forest_hills", "mutated_desert", "mutated_extreme_hills", "mutated_extreme_hills_with_trees", "mutated_forest", "mutated_ice_flats", "mutated_jungle", "mutated_jungle_edge", "mutated_mesa", "mutated_mesa_clear_rock", "mutated_mesa_rock", "mutated_plains", "mutated_redwood_taiga", "mutated_redwood_taiga_hills", "mutated_roofed_forest", "mutated_savanna", "mutated_savanna_rock", "mutated_swampland", "mutated_taiga", "mutated_taiga_cold", "ocean", "plains", "redwood_taiga", "redwood_taiga_hills", "river", "roofed_forest", "savanna", "savanna_rock", "sky", "smaller_extreme_hills", "stone_beach", "swampland", "taiga", "taiga_cold", "taiga_cold_hills", "taiga_hills",
                             NULL };

    for(int i = 0; biomes[i] != NULL; i++) {        // changed loop control
        printf("%s\n", biomes[i]);                  // changed argument passed
    }
}

int main(void){
    CreateBiomes();
    return 0;
}

请注意,此函数不会有太大用处,因为函数返回后将无法访问biomes(本地变量)。

答案 2 :(得分:0)

尝试这种方式,您的代码应该可行。

void CreateBiomes()
{

   int BIOME_COUNT = 63;
   char *biomes[]= {"beaches", "birch_forest", "birch_forest_hills", "cold_beach", "deep_ocean", "desert", "desert_hills", "extreme_hills", "extreme_hills_with_trees", "forest", "forest_hills", "frozen_ocean", "frozen_river", "hell", "ice_flats", "ice_mountains", "jungle", "jungle_edge", "jungle_hills", "mesa", "mesa_clear_rock", "mesa_rock", "mushroom_island", "mushroom_island_shore", "mutated_birch_forest", "mutated_birch_forest_hills", "mutated_desert", "mutated_extreme_hills", "mutated_extreme_hills_with_trees", "mutated_forest", "mutated_ice_flats", "mutated_jungle", "mutated_jungle_edge", "mutated_mesa", "mutated_mesa_clear_rock", "mutated_mesa_rock", "mutated_plains", "mutated_redwood_taiga", "mutated_redwood_taiga_hills", "mutated_roofed_forest", "mutated_savanna", "mutated_savanna_rock", "mutated_swampland", "mutated_taiga", "mutated_taiga_cold", "ocean", "plains", "redwood_taiga", "redwood_taiga_hills", "river", "roofed_forest", "savanna", "savanna_rock", "sky", "smaller_extreme_hills", "stone_beach", "swampland", "taiga", "taiga_cold", "taiga_cold_hills", "taiga_hills", "void"};
   int i;
   for(i = 0; i < BIOME_COUNT-1; i++)
   {
    printf("%s\n", biomes[i]);
   }

   return;
}