user3885166
user3885166

Reputation: 215

Seg fault while checking argv for an ampersand

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;
}

Upvotes: 0

Views: 158

Answers (1)

nbro
nbro

Reputation: 15837

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

Upvotes: 4

Related Questions