我在使用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)。为什么会这样呢?
发布于 2017-06-20 08:09:14
尝试替换声明
const char *biomes[BIOME_COUNT][STRING_LENGTH+1]如下所示。
const char biomes[BIOME_COUNT][STRING_LENGTH+1]发布于 2017-06-20 08:39:56
您的数组是一个二维指针数组,您在printf循环中索引了太多的指针。
我已经将其更改为一维指针数组,并删除了硬编码的大小。相反,我用一个"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是一个局部变量,在函数返回后将无法访问。
发布于 2017-06-20 08:46:59
尝试这样做,您的代码应该可以工作。
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;
}https://stackoverflow.com/questions/44647303
复制相似问题