sorin.va
sorin.va

Reputation: 41

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.

Upvotes: 0

Views: 84

Answers (1)

mcleod_ideafix
mcleod_ideafix

Reputation: 11428

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).

Upvotes: 2

Related Questions