-10
char * str = "012";

int main(int argc, char * argv[]) {
  if (str == argv[1]) {
    printf("success");
  }
}

output:

./test 0x400634

Then I should get the output success.

But I cannot get any output.How can I compare those two address and print that they are equal. please help...

Derek Wang
  • 10,098
  • 4
  • 18
  • 39
Adam
  • 27
  • 6
  • 4
    Why would you want these addresses to be equal? Quite plainly they can *never* be equal -- the string is part of your program, and the arguments are provided by the environment. – Kerrek SB Sep 25 '16 at 17:33
  • `argv[1]` is "0x400634" at some address, not "012" at 0x400634 – Daniel Sep 25 '16 at 17:37
  • the address of str is 400634.I am just passing this as a command line argument and again comparing it with str address in the program and want to print success. – Adam Sep 25 '16 at 17:42
  • 2
    In C you compare the *content* of two strings with `strcmp` library function. The string *addresses* will never be equal as commented already. – Weather Vane Sep 25 '16 at 18:08

2 Answers2

2

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: https://stackoverflow.com/a/20654510/2830652)

if ((long)str == strtol(argv[1], NULL, 16))
{
    printf("success");
}

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

Community
  • 1
  • 1
sokkyoku
  • 2,161
  • 1
  • 20
  • 22
  • printf("%016x",str); the output would be 400634. So I want to send this address to a command line argument and in the program I want to compare and show them equal.I tried strcmp but it never works and gives me segmentation fault. – Adam Sep 25 '16 at 17:37
  • Does `printf("%016x", str);` print the exact same address every time you run your program? `strcmp` not working is because of what I mention in my answer. `argv[1]` does not contain what you think it contains. – sokkyoku Sep 25 '16 at 17:43
  • Almost str prints you the same address.So I want compare content in argv[1] with str address .Is that possible?? – Adam Sep 25 '16 at 17:48
  • It's possible but I'm not convinced it's useful. If you want to compare them, I'll add code in my answer – sokkyoku Sep 25 '16 at 17:51
  • it gives me error ,because str is a pointer and strtol will give an integer – Adam Sep 25 '16 at 18:03
  • With a cast to long it should be better (edited the code in my answer) – sokkyoku Sep 25 '16 at 18:05
0

Comparing memory addresses can get a bit tricky.

I will start with the simple case and move on to 2 more advanced cases.

For full explanation of the code, I've posted a youtube video.

https://www.youtube.com/watch?v=Yk9ROvIQCts

However, the comments in this code are hopefully enough by themselves.

#include <stdio.h> //:for: printf(...)

//: Generic function pointer type:
//: Only DATA pointers can be cast to void*
//: Function pointers CANNOT be cast to void*
typedef void ( * GenFunc ) ( void );

struct global_state{

    int val;

}GS;

int  Func_A(   void   ){ return 5    ; }
void Func_B( int  val ){ GS.val = val; } 

int main( void ){
    printf("[BEG :main]\n");



    //|1|1|1|1|1|1|1|1|1|1||1|1|1|1|1|1|1|1|1|1|//
    //| Compare Addresses of KNOWN types:      |//
    //|1|1|1|1|1|1|1|1|1|1||1|1|1|1|1|1|1|1|1|1|//
    int a = 6;                             //|1|//
    int b = 6;                             //|1|//
                                           //|1|//
    int* ptr_1 = &( a );                   //|1|//
    int* ptr_2 = &( b );                   //|1|//
    int* ptr_3 = &( a );                   //|1|//
                                           //|1|//
    if( ptr_1 != ptr_2 ){                  //|1|//
        printf("[ptr : <> ]\n");           //|1|//
    };;                                    //|1|//
    if( ptr_1 == ptr_3 ){                  //|1|//
        printf("[ptr : == ]\n");           //|1|//
    };;                                    //|1|//
    //|1|1|1|1|1|1|1|1|1|1||1|1|1|1|1|1|1|1|1|1|//



    //|2|2|2|2|2|2|2|2|2|2||2|2|2|2|2|2|2|2|2|2|//
    //| Compare addresses of function pointers |//
    //| without casting to (void*). It is      |//
    //| undefined behavior to cast function    |//
    //| pointers to (void*).                   |//
    //|2|2|2|2|2|2|2|2|2|2||2|2|2|2|2|2|2|2|2|2|//
    GenFunc gfp_1 = (GenFunc) &( Func_A ); //|2|//
    GenFunc gfp_2 = (GenFunc) &( Func_B ); //|2|//
    GenFunc gfp_3 = (GenFunc) &( Func_A ); //|2|//
    if( gfp_1 != gfp_2 ){                  //|2|//
        printf("[gfp : <> ]\n");           //|2|//
    };;                                    //|2|//
    if( gfp_1 == gfp_3 ){                  //|2|//
        printf("[gfp : == ]\n");           //|2|//
    };;                                    //|2|//
    //|2|2|2|2|2|2|2|2|2|2||2|2|2|2|2|2|2|2|2|2|//



    //|3|3|3|3|3|3|3|3|3|3||3|3|3|3|3|3|3|3|3|3|//
    //| wglGetProcAddress returns generic      |//
    //| function pointer. The documentation    |//
    //| says we need to check for:             |//
    //|                                        |//
    //| 0x00, 0x01, 0x02, 0x03, or -1          |//
    //|                                        |//
    //| for failure.                           |//
    //|                                        |//
    //| PRETEND gfp_1 was fetched using        |//
    //| wglGetProcAddress.                     |//
    //| (Note: Zero is special and does NOT )  |//
    //| (      require a cast.              )  |//
    //|3|3|3|3|3|3|3|3|3|3||3|3|3|3|3|3|3|3|3|3|//
    if(                                    //|3|//
        (gfp_1 ==           0  ) ||        //|3|//
        (gfp_1 == (GenFunc) 0x1) ||        //|3|//
        (gfp_1 == (GenFunc) 0x2) ||        //|3|//
        (gfp_1 == (GenFunc) 0x3) ||        //|3|//
        (gfp_1 == (GenFunc)  -1) ||        //|3|//
    0){                                    //|3|//
        printf("[Failure!]");              //|3|//
    };;                                    //|3|//
    //|3|3|3|3|3|3|3|3|3|3||3|3|3|3|3|3|3|3|3|3|//



    printf("[END :main]\n");
}
KANJICODER
  • 3,611
  • 30
  • 17