C question: why there's an "i" here ? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 2

C question: why there's an "i" here ?

ā€‹intā€‹Ā ā€‹mainā€‹(ā€‹intā€‹Ā argc,Ā stringĀ argv[ ]) ā€‹{ ā€‹Ā Ā Ā Ā ā€‹intā€‹Ā trackdigitĀ =Ā ā€‹0ā€‹; ā€‹Ā Ā Ā Ā ā€‹ifā€‹Ā (argcĀ ==Ā ā€‹2ā€‹) ā€‹Ā Ā Ā Ā { ā€‹Ā Ā Ā Ā Ā Ā Ā Ā ā€‹forā€‹Ā (ā€‹intā€‹Ā iĀ =Ā ā€‹0ā€‹,Ā nĀ =Ā ā€‹strlenā€‹(argv[ā€‹1ā€‹]);Ā iĀ <Ā n;Ā i++) ā€‹Ā Ā Ā Ā Ā Ā Ā Ā { ā€‹Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ā€‹ifā€‹Ā (ā€‹isdigitā€‹(argv[ā€‹1ā€‹][i])) /////// here ā€‹Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { ā€‹Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā trackdigitĀ +=ā€‹1ā€‹; ā€‹Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } And another question: why when im doing normal if else statement and trying to check if argv[1] is digit without adding "i" it doesn't work and give me segfault

17th Feb 2022, 4:51 PM
Abdallah Ashraf
5 Respostas
+ 5
Command line arguments are strings, but isdigit() is only defined for single characters (which a string is composed of), hence the loop and the additional index. The expression argv[1][i] refers to the (i + 1)th character in the second command line argument, so the loop counts how many digits are present in that string.
17th Feb 2022, 5:13 PM
Shadow
Shadow - avatar
+ 2
May I know Is there string type in c? Is it new feature? Shadow
17th Feb 2022, 6:12 PM
Jayakrishna šŸ‡®šŸ‡³
+ 1
JayakrishnašŸ‡®šŸ‡³ Not that I know of, I assumed it to be a typedef for char* maybe.
17th Feb 2022, 6:28 PM
Shadow
Shadow - avatar
+ 1
Jayakrishna, The below thread might have answer to your doubt. The code in that thread includes cs50 header, perhaps 'string' was a custom type introduced in that header. https://www.sololearn.com/Discuss/2984614/?ref=app
17th Feb 2022, 9:23 PM
Ipang
+ 1
edit: Oh. Thats useful. Ipang , Shadow thanks you both for the clarification.. I guess, for OP also..
18th Feb 2022, 9:40 AM
Jayakrishna šŸ‡®šŸ‡³