Reputation: 1117
I have found the following code, and I do not understand what it is or how it works. I have only seen argv[n]
(argv with an integer index) in C before, never with a character literal like argv['A']
.
if(argc != 100) return 0;
if(strcmp(argv['A'],"\x00")) return 0;
if(strcmp(argv['B'],"\x20\x0a\x0d")) return 0;
printf("Stage 1 clear!\n");
What does this do? Could you explain why it works?
Upvotes: 3
Views: 992
Reputation: 2937
Technically, this is valid in C for accessing an element of the argv (argv[65]
).
Edit: as John Ballinger noted in the comments, this is only if your C implementation maps character literals to their ASCII values, which is not required by the standard. However, most C implementations do, so I would assume the author of the code was using this assumption.
This is because in C, all character literals can act as numeric literals.
Granted, if you are expecting 65+ elements in your argv, something is probably wrong. And even so, using 'A'
and 'B'
for the indices 65 and 66 wouldn't make sense unless you had some specific association of your argv with letters.
TL;DR it is an index, but the code is incredibly abnormal and probably intentionally obfuscated
Upvotes: 5
Reputation: 234705
'A'
is just a way of specifying a value with an int
type (which, given that you've passed an alphanumeric character, must be capable of fitting into a char
on your platform.) The value is implementation defined, although in ASCII, it's 65.
So argv['A']
is rather like int n = 'A'; argv[n];
Whether or not the program is equipped with that many command line arguments (argc
will tell you that and if(argc != 100)
is an insufficient check) is another matter.
That all said, it's possible to call main
from itself in C; perhaps the command line arguments are introduced then, as an exercise in obfuscation.
Upvotes: 2