Pointer variable Address

Discussion in 'C Programming' started by pavunkumar, Mar 10, 2009.

  1. pavunkumar

    pavunkumar Guest

    Dear Friend

    Please Explain about this things
    I have character pointer variable

    char *p="string";
    char *s ="string";

    If I print the both variable of the address
    It is print same address why ?
     
    pavunkumar, Mar 10, 2009
    #1
    1. Advertising

  2. pavunkumar

    H Vlems Guest

    On 10 mrt, 07:20, pavunkumar <> wrote:
    > Dear Friend
    >
    >                    Please Explain about this things
    > I have character pointer variable
    >
    > char *p="string";
    > char *s ="string";
    >
    > If I print the both variable of the address
    > It is print same address why ?


    The text "string" is a literal (composed of characters though that is
    not important).
    The compiler stores literals in a read-only part of the codefile.
    Because it is
    read-only the compiler will store the value only once and refer other
    occurances to the same address.
    That is why both pointers return the same address, because they both
    point to the same literal.

    (I have a strong feeling I've been doing your homework)
     
    H Vlems, Mar 10, 2009
    #2
    1. Advertising

  3. pavunkumar

    James Kuyper Guest

    pavunkumar wrote:
    > Dear Friend
    >
    > Please Explain about this things
    > I have character pointer variable
    >
    > char *p="string";
    > char *s ="string";
    >
    > If I print the both variable of the address
    > It is print same address why ?



    Section 6.4.5p5 of the standard explains how the characters that are
    specified by a string literal are used to initialize an array. Then, in
    paragraph 6, it goes on to say "It is unspecified whether these arrays
    are distinct provided their elements have the appropriate values. ...".

    It's not just that s == p; you can even have two different string
    literals overlapping. If we add the following declaration:

    char *t = "this is a string";

    It's quite possible for a conforming implementation to have the string
    pointed at by p and q be the the final portion of the same string that t
    points at, so that t+10 == p; there are real implementations which do
    precise this, to save space.
     
    James Kuyper, Mar 10, 2009
    #3
  4. pavunkumar

    James Kuyper Guest

    H Vlems wrote:
    > On 10 mrt, 07:20, pavunkumar <> wrote:
    >> Dear Friend
    >>
    >> Please Explain about this things
    >> I have character pointer variable
    >>
    >> char *p="string";
    >> char *s ="string";
    >>
    >> If I print the both variable of the address
    >> It is print same address why ?

    >
    > The text "string" is a literal (composed of characters though that is
    > not important).
    > The compiler stores literals in a read-only part of the codefile.


    Not necessarily. It's not uncommon for string literals to be writable.
    The standard deliberately leaves it unspecified whether or not string
    literals can be written to, because there's a fair number of real-world
    implementations which do it either way.

    Portable code should assume that they are not writable, but not all code
    needs to be portable.

    > Because it is
    > read-only the compiler will store the value only once and refer other
    > occurances to the same address.


    Again, the standard permits them to be the same; it doesn't require it,
    and many compilers don't bother merging them.
     
    James Kuyper, Mar 10, 2009
    #4
  5. pavunkumar

    Nelu Guest

    On 2009-03-10, pavunkumar <> wrote:
    >
    > Dear Friend
    >
    > Please Explain about this things
    > I have character pointer variable
    >
    > char *p="string";
    > char *s ="string";
    >
    > If I print the both variable of the address
    > It is print same address why ?


    Because the standard doesn't require them to have different addresses.

    Why would you have two copies of the exact same immutable value?

    Modifying a string literal invokes undefined behavior so it shouldn't be
    done in code, which allows compilers to intern (I think this is the right
    word) string literals at compilation time.


    --
    Ioan - Ciprian Tandau
    tandau _at_ freeshell _dot_ org
     
    Nelu, Mar 10, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jimjim
    Replies:
    16
    Views:
    844
    Jordan Abel
    Mar 28, 2006
  2. Sourav
    Replies:
    25
    Views:
    692
  3. Scott
    Replies:
    6
    Views:
    382
    Luke Meyers
    Mar 27, 2006
  4. Stanley Rice

    Address of array && address of pointer to array

    Stanley Rice, Sep 14, 2011, in forum: C Programming
    Replies:
    33
    Views:
    1,147
    Keith Thompson
    Sep 20, 2011
  5. junyangzou
    Replies:
    8
    Views:
    183
    junyangzou
    Aug 30, 2013
Loading...

Share This Page