我正在创建一个包含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]
鉴于每个子阵列的大小不同。
答案 0 :(得分:3)
easy 解决方案是使子数组的大小相同,填充“”或“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 };