Warm tip: This article is reproduced from stackoverflow.com, please click
c

What is the meaning of "argv['A']" in c?

发布于 2020-03-27 10:16:59

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?

Questioner
user1208081
Viewed
99
Edward Minnix 2019-07-03 21:41

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