简体   繁体   中英

Why the program returns acces violation?

Why does this program returns access violation every time I run it?

void sort_lines(char *tp[], int n)
{
    int sortat = 0, i;
    char *temp;
    while (!sortat)
    {
        sortat = 1;
        for (i = 0; i<n-1; i++)
            if (strcmp(tp[i], tp[i+1])<0)
            {
                temp = tp[i];
                tp[i] = tp[i+1];
                tp[i+1] = temp;
                sortat = 0;
            }
    }  
}

int main()
{
    int i = 0;
    char *sir[7] = { "mama", "mananca", "mancare", "facuta", "doar", "de", "ea" };
    int m = 7;
    for (i = 0; i < m; i++)
    {
        printf("%s\n", sir[i]);
    }
    sort_lines(sir, m);
    for (i = 0; i < m; i++)
    {
        printf("%s\n", sir[i]);
    }
    return 0;
}

Seems that if i restart my pc it works 10 times out of 11. It is driving me nuts. I dont really understand why.

I think you have have forgotten to include stdio.h and string.h in your program. With that two includes, your program works for me (ah! well, and the ; after return 0 at the last line).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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