Are these two statements same?

Discussion in 'Meet and Greet' started by amitnvaidya, Oct 15, 2009.

  1. amitnvaidya

    amitnvaidya New Member

    Joined:
    Oct 15, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    int *ptr = "NULL"; // Here is a statement

    int *ptr; // Here is a set of two statements
    *ptr = "NULL";

    /*
    when
    you print the first
    it gives 0
    and second if you print
    it gives linking error
    why so
    */
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    NULL should be NULL and not "NULL" as "NULL" one is a string literal
     
  3. amitnvaidya

    amitnvaidya New Member

    Joined:
    Oct 15, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Thats agreed... whats the result in the following case.

    int *ptr= NULL;
    printf("%d",ptr);



    int *ptr;
    *ptr = NULL;
    printf("%d",ptr);
     
  4. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    In the first case, you assign NULL to the pointer (the address), NOT to the value pointed by it.
    In the second case, you assign NULL to the value pointed by the pointer.
    So, the cases are not equivalent.

    These are equivalent :

    int *ptr = NULL;
    printf("%d", ptr);

    int *ptr;
    ptr = NULL;
    printf("%d",ptr);

    And should have equal behaviour on compilation. (Try them !)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice