1

我正在创建一个包含 C 中字符串数组的数组。我有一个名为 conditionType 的枚举,用于通过条件数组的第一个索引进行访问。

enum conditionType {
  CLEAR = 0,
  OVERCAST,
  CLOUDY,
  RAIN,
  THUNDERSTORM,
  SNOW
};

int conditionsIndex[6] = { 
  CLEAR, OVERCAST, CLOUDY, RAIN, THUNDERSTORM, SNOW}; 

const char *conditions[][count] = {
  // CLEAR
  {
    "Clear"  }
  ,
  // OVERCAST
  {
    "Overcast","Scattered Clouds", "Partly Cloudy"  }
  ,
  // CLOUDY
  { 
    "Shallow Fog","Partial Fog","Mostly Cloudy","Fog","Fog Patches","Smoke"  }
  ,
  // RAIN
  {
    "Drizzle",
    "Rain",
    "Hail",
    "Mist",
    "Freezing Drizzle",
    "Patches of Fog",
    "Rain Mist",
    "Rain Showers",
    "Unknown Precipitation",
    "Unknown",
    "Low Drifting Widespread Dust",
    "Low Drifting Sand"  }
  ,
  // THUNDERSTORM
  {
    "Thunderstorm",
    "Thunderstorms and Rain",
    "Thunderstorms and Snow",
    "Thunderstorms and Ice Pellets",
    "Thunderstorms with Hail",
    "Thunderstorms with Small Hail",
    "Blowing Widespread Dust",
    "Blowing Sand",
    "Small Hail",
    "Squalls",
    "Funnel Cloud"  }
  ,
  // SNOW
  {
    "Volcanic Ash",
    "Widespread Dust",
    "Sand",
    "Haze",
    "Spray",
    "Dust Whirls",
    "Sandstorm",
    "Freezing Rain",
    "Freezing Fog",
    "Blowing Snow",    "Snow Showers",
    "Snow Blowing Snow Mist",
    "Ice Pellet Showers",
    "Hail Showers",
    "Small Hail Showers",
    "Snow",
    "Snow Grains",
    "Low Drifting Snow",
    "Ice Crystals",
    "Ice Pellets"  }
};

我是 C 新手,我想知道这条线上需要多少数字

const char *conditions[][count] 

假设每个子数组的大小不同。

4

1 回答 1

3

简单的解决方案是使子数组大小相同,用 "" 或 NULL 填充。

更好的解决方案是分别声明每个子数组,并制作conditions一个指向字符串指针数组的指针数组。字符串指针的每个子数组都以 NULL 结尾以指示其长度。

#include <stdlib.h>
const char *condCLOUDY[] = { "Shallow Fog", "Partial Fog", "Mostly Cloudy", "Fog",
    "Fog Patches", "Smoke", NULL };
const char *condRAIN[] = { "Drizzle", "Rain", "Hail", "Mist", NULL };
// etc.
const char **conditions[] = { condCLEAR, condOVERCAST, condCLOUDY, condRAIN, 
     condTHUNDERSTORM, condSNOW, NULL };
于 2013-08-14T18:26:26.930 回答