Does static index of array will change the array?

I want to break a path name into a sequence using strtok. But I encount a question, my code is showed below:

int main(void)
{
    char test[] = "/hoe/wahoo/Documents/hello/test";
    char *str[8]; 
    int index = 0;
    int count = 0;

    str[index] = strtok(test, "/");
    while (str[index] && *str[index]) {
        index++;
        str[index] = strtok(NULL, "/");
    }

    for (index=0; index<8; index++) {
        if (str[index] && *str[index]) {
            count++;
        }
    }

    printf("%dn", count);

    return 0;
}

Obviously, I get a error segmentation error, and I know the source of the error. Once, I change the int index = 0; to static index = 0; in the line 5. I find that it does work!!

So why? It seems like I change the index of array into static, that the array will be static.


Source: c#

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.