Comparing two memory addresses and showing them equal

I think you are slightly confused as to how strings work in C.

Strings are arrays of characters and two strings that have the same content do not necessarily have the same address.

Also, argv only contains strings. If you pass a number to your program, it will be interpreted as a string, not a number. Thus when you are comparing argv[1] to str, even if you could know what address str will be at (you can’t), you would be comparing strs address to the address of argv[1], not its contents.

If you wish to extract argv[1] as a number, use strtol (as mentioned here:

if ((long)str == strtol(argv[1], NULL, 16))

Will allow you to compare the right data, just omit the 0x part of the address when you pass it to your program.

Browse More Popular Posts

Leave a Comment