problem with argv in c


I'm writing a program to go through one of the arguments and store them in a variable, but I get a segmentation error right in this loop:

 for(int i=0; argv[1][i] != "
 for(int i=0; argv[1][i] != "%pre%" ;i++){
" ;i++){ }

How else could argv walk to avoid this error?

asked by kalia 02.05.2017 в 19:25

1 answer


You make an incorrect comparison. Surely, the compiler will give you a warning ( warning ).

argv[1][i] != "char" are comparing a argv[1][i] with a pointer . Well, actually the compiler is promoting int to int , so, actually, you're comparing a "..." with a pointer .

For what you want to do, you can use

argv[1][i] != 0


argv[1][i] != '
argv[1][i] != 0
' // Comillas simples '

Remember that, in C, strings enclosed in double quotes %code% are no more than arrays of characters. By using them, you actually use your address , that is, a pointer .

answered by 02.05.2017 / 19:33