Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Pointer variable Address

Reply
Thread Tools

Pointer variable Address

 
 
pavunkumar
Guest
Posts: n/a
 
      03-10-2009

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 ?




 
Reply With Quote
 
 
 
 
H Vlems
Guest
Posts: n/a
 
      03-10-2009
On 10 mrt, 07:20, pavunkumar <pavun....@gmail.com> 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)
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      03-10-2009
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.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      03-10-2009
H Vlems wrote:
> On 10 mrt, 07:20, pavunkumar <pavun....@gmail.com> 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.
 
Reply With Quote
 
Nelu
Guest
Posts: n/a
 
      03-10-2009
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57