开发者

Problems with accessing char***

Im trying to access a char * in strcasestr and it seems to have no effect Heres the code:

#define NUMOFADDTYPES 5

const char* milkAdditions[] = {"Cream", "Half-and-half", "Whole-Milk", 
                   "Part-skim", "skim", "non-dairy", NULL};
const char* syrupAdditions[] = {"Vanilla", "Almond", "Raspberry", NULL};
const char* sweetenerAdditions[] = {"white-sugar", "sweetener", 
                    "Raw-cane", "honey", NULL};
const char* spiceAdditions[] = {"Cinnamon", "cardamon", NULL};
const char* alcoholAdditions[] = {"Brandy", "Rum", "Whiskey", 
                  "Aquavit", "Kahlua", NULL};

const char** additions[] = {milkAdditions, syrupAdditions, 
                sweetenerAdditions, spiceAdditions, 
                alcoholAdditions};



  char *ptr;
  int i, j;

  printf("Dump of additions[j][i]\n");
  for(j = 0; j < NUMOFADDTYPES; j++)
    {
      for(i = 0; (additions[j])[i] != NULL; i++)
    {
      printf("%d %d\t%s\n", j, i, additions[j][i]);
      if((ptr = strcasestr((additions[j])[i], string)) != NULL)
      {
        ptr += strlen((additions[j])[i]);
        getVolume(ptr);
      }
    }

Output is:

Dump of additions[j][i]
0 0 Cream
0 1 Half-and-half
0 2 Whole-Milk
0 3 Part-skim
0 4 skim
0 5 non-dairy
1 0 Vanilla
1 1 Almond
1 2 Raspberry
2 0 white-sugar
2 1 sweetener
2 2 Raw-cane
2 3 honey
3 0 Cinnamon
3 1 cardamon
4 0 Brandy
4 1 Rum
4 2 Whiskey
4 3 Aquavit
4 4 Kahlua

any help would be g开发者_JAVA百科reatly appreciated

Edit:

sorry didnt add

  char string[] = "AcceptAdditions: Cream;lots white-sugar;dash\r\n\r\n";
  getAddition(string);

Solved


Sorry guys I misread the man page and was searching the substring with the main string

char *strcasestr(const char *haystack, const char *needle);

Thanks for the answers anyways


After edit:


You are comparing each of "Cream", "Half and half", ... "Kahlua" to "AcceptAdditions: Cream;lots white-sugar;dash\r\n\r\n".

The strcasestr will never find a match!

I think you want to break your string in smaller parts and check the little parts.

char string[] = "AcceptAdditions: Cream;lots white-sugar;dash\r\n\r\n";
const char *accepted[] = {"cream", "white-sugar", "dash"};

and then check each item against each accepted element

      for (int k = 0; k < sizeof accepted / sizeof *accepted; k++) {
          if ((ptr = strcasestr((additions[j])[i], accepted[k])) != NULL)
          {
            //ptr += strlen((additions[j])[i]);
            getVolume(ptr);
          }
      }



char *strcasestr(const char *haystack, const char *needle);

The strcasestr() function finds the first occurrence of the substring needle in the string haystack.

So, isn't it need to be

strcasestr(string, (additions[j])[i])

instead of

strcasestr((additions[j])[i], string)

?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜