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?
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
Technically, it is implementation-dependent which
argv
element is accessed. Implementations are not required to use ASCII to map characters to integers, and some don't."all character literals can act as numeric literals". Character literals have type
int
anyway.@Gerhardh. Yes, but a lot of people forget that chars are an integral numeric type. So I was just being explicit with that.