1

In my current code, I am trying to pass in a file path, separate the file path by "/" , and then checking through each delimited value against every element in an array named categories using a for loop. If any element in the array matches as a substring using strstr(), it will return the value of the element, if not it will return "Others".
I managed to produce the output that I want, however I realise that whenever there is an even number of elements in categories array, the output will be 1 of the value from the file path such as
If the file path is

"/a/b/Lib/Contact/c"

If the categories are

char *categories[] = {"Library","Applications","Contact","dsdfd"};  

the output will be b.
Here is my code for your reference:

const char *categorize(char*path)
{
    int i = 0;
    char str[1024];
    char *token;
    char *delim = "/";
    char *categories[] = {"Library","Applications","Contact","dsdfd"};
    bool check = false;

    strcpy(str,path);
    token = strtok(str,delim);
    while (token !=NULL)
    {
        if(check == true)
            break;

         //printf("%s\n",token);
        for (i = 0; i <(sizeof(categories)/sizeof(char*)); i++)
        {
            if(categories[i] == NULL)
                break;
            if(strstr(token, categories[i]))
            {
                check = true;
                break;
            }
        }
        token = strtok (NULL, delim);
    }
    if (check == true)
        return "Others";
    else
    return categories[i];

}

Edit: I have encountered another problem. I tried using a test case if the file path does not contain any substring. It should return "Others", however it is returning me "/".
Edit2: I have sort of solved the problem by changing the if statement to check == true. However, I realise that my code here is not good which @LưuVĩnhPhúc and @fasked mentioned in the comments and I would hope to possibly fixed it.

user2541163
  • 717
  • 2
  • 7
  • 22
  • 4
    `sizeof(categories)` is not the length of your array – amdixon Dec 30 '13 at 04:18
  • .. sizeof(categories)/sizeof(char*) – AndersK Dec 30 '13 at 04:23
  • Also you return pointer to local C-string. Most of the compilers will not allocated strings on the stack, but they are not obligated. – fasked Dec 30 '13 at 04:35
  • @fasked , sorry can you explain it further? I dont really understand what u are trying to get across. sry – user2541163 Dec 30 '13 at 04:40
  • @user2541163 Look through accepted answers http://stackoverflow.com/questions/1224042/returning-a-pointer-to-an-automatic-variable and http://stackoverflow.com/questions/6897914/c-warning-function-returns-address-of-local-variable. The first defines the problem and the second gives the answer. – fasked Dec 30 '13 at 04:52
  • ok I will look at it thanks. Also, I have encountered another error described at my Edit – user2541163 Dec 30 '13 at 04:55
  • @user2541163 you're returning `categories[i]` which is a local variable. Its scope will be ended when the function ends and nothing guaranties that it still remains on stack after that. For string literals maybe it will be stored in read-only memory but you shouldn't rely on that. Returning by pointer/reference from parameter is recommended – phuclv Dec 30 '13 at 04:57
  • Consider using something like [lsearch](http://ftp.gnu.org/old-gnu/Manuals/glibc-2.2.3/html_chapter/libc_9.html) (if available) rather that rolling out your own search. – Noufal Ibrahim Dec 30 '13 at 05:08

1 Answers1

0

Change your code to

 for (i = 0; i <(sizeof(categories)/sizeof(char*)); i++)

sizeof(categories) will give your 16 bytes (size of 4 char*) you will have to devide that with each char* size to get number of elements in categories;

Digital_Reality
  • 4,488
  • 1
  • 29
  • 31