Pointer variable Address

P

pavunkumar

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 ?
 
H

H Vlems

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)
 
J

James Kuyper

pavunkumar said:
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.
 
J

James Kuyper

H said:
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.
 
N

Nelu

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top