Reputation: 215
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
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