1

I'm creating an array that contains arrays of strings in C. I have a enum called conditionType that's used to access through the first index of the conditions array.

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"  }
};

I'm new to C and I was wondering what number count would need to be on this line

const char *conditions[][count] 

given that each sub-array is a different size.

theblueone
  • 681
  • 1
  • 6
  • 9

1 Answers1

3

The easy solution is to make the sub-arrays the same size, filling with "" or NULL.

The better solution is to declare each sub-array separately and make conditions an array of pointers to an array of string pointers. Each sub-array of string pointer terminated with a NULL to indicate its length.

#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 };
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256