【问题标题】:Iterate through 2 dimensional array in C遍历C中的二维数组
【发布时间】:2013-09-17 02:20:16
【问题描述】:

我有一个包含字符串值的二维数组。我在迭代时使用枚举来跟踪数组的第一个索引。我试图找到给定字符串匹配的枚举(数组的第一维)。现在,我遍历数组和检查值的方式似乎并不是 100% 有效,因为它会返回错误的枚举。有谁知道我做错了什么,或者知道基于第二个匹配字符串获取第一个数组索引的更好方法?

注意:我使用的是 Arduino,并且使用的是字符串对象而不是 char*。

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

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

    const char *conditions[][20] = {
      // CLEAR
      {
        "Clear"          }
      ,
      // OVERCAST
      {
        "Partly Cloudy"  }
      ,
      // CLOUDY
      { 
        "Shallow Fog",
        "Partial Fog",
        "Mostly Cloudy",
        "Fog","Overcast",
        "Scattered Clouds" }
      ,
      // 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"          }
    };




      int currentCondition;
      for ( int i = 0; i < ( sizeof(conditionsIndex) / sizeof(int) ); i++ ) {
         int idx = conditionsIndex[i];
         for (int j = 0; j < ( sizeof(conditions[idx]) / sizeof(String) ); j++ ) {
         if ( forecast.equals(conditions[idx][j]) ) {
            currentCondition = idx;
           }
         }
       }

【问题讨论】:

  • 这里有什么问题吗,j

标签: c string multidimensional-array enums arduino


【解决方案1】:

首先,conditions 是一个 6 x 20 的字符串指针矩阵。某些行,例如 cloudy 仅分配了一个指针(共 20 个)。所以循环需要测试一个空指针并转到下一行。

要编写你的代码,但在伪代码中应该是这样的:

 memfill(conditions, 0, 6*20*sizeof(char *));
 for (i = 0 to 5)

   for (j = 0 to 20)

       if (conditions[i][j] == NULL) {break;}

       print("%s/n", conditions[i][j]);
       // test/compare strings, i is the enum value if a match
       // return i;

   end for j

end for i

【讨论】:

    【解决方案2】:

    您的代码的另一个问题是您很可能会用完内存。你可能想看看progmem.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-04
      • 1970-01-01
      • 2017-10-18
      相关资源
      最近更新 更多