简体   繁体   English

遍历C中的二维数组

[英]Iterate through 2 dimensional array in C

I have a 2 dimensional array that contains string values. 我有一个包含字符串值的二维数组。 I am using an enum to keep track of the first index of the array when iterating. 我使用一个枚举来跟踪迭代时数组的第一个索引。 I am trying to find the enum (first dimension of an array) that a given string matches. 我试图找到给定字符串匹配的枚举(数组的第一维)。 Right now, my the way I'm iterating over the arrays and checking for values doesn't seem to seem to work 100% of the time because it will return the wrong enum. 现在,我遍历数组并检查值的方式似乎无法100%地起作用,因为它会返回错误的枚举。 Does anyone see what I'm doing wrong, or know of a better way to get the index of the first array based on a matched string in the second? 有人看到我在做什么错,还是知道一种更好的方法来根据第二个数组中的匹配字符串获取第一个数组的索引?

NOTE: I am using Arduino, and am using String objects instead of char*. 注意:我正在使用Arduino,并且正在使用String对象而不是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;
           }
         }
       }

First of all, conditions is a 6 by 20 matrix of pointers to strings. 首先,条件是指向字符串的指针的6 x 20矩阵。 Some of the rows, such as cloudy only has one pointer (out of the 20) assigned. 某些行(例如阴天)仅分配了一个指针(在20个指针中)。 So the loop needs to test for a null ptr and go to the next row. 因此,循环需要测试一个空的ptr并转到下一行。

I'm not going to write your code, but in psuedo-code it should be something like: 不会编写您的代码,但是在伪代码中应该是这样的:

 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

Another issue with your code is that you are very likely to run out of ram. 代码的另一个问题是您很可能用完了ram。 You might want to have a look into progmem. 您可能需要看一下progmem。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM