Seg fault while checking argv for an ampersand

184 Views Asked by At

I have a function that checks whether argv contains ampersand at the end (shell purposes) and it gives a seg fault for seemingly no reason.. Thanks in advance

length(char **argv) calculates length of argv.

int ampersandCheck(char **argv){
    int n = length(argv);
    int i = 0;
    while(argv[n][i] != '\0'){
        if(argv[n][i] == '&' && argv[n][i+1] == '\0') return 1;
        i++;
    }

    return 0;
}
1

There are 1 best solutions below

1
On

This is because you are accessing the position n in argv[n][i]. That position does not exist. The last position you can access is n - 1