When u say char p[]="hello";

memory to array is allocated on stack and "hello" is written into that
array. so u can modify "hello" by saying p[0]= 'y';

=====================================================

when u say char *x="hello";

memory to x is allocated on stack, but string "hello" is placed in the read
only memory section(sometimes called as literal section)
and address to "hello" is stored in variable x. this type of string are
called as read only strings. so u can modify x, but not the contents of the
constant string "hello".

So x=p; // valid , modified variable on stack
but x[0]='z' // invalid, tried to modify the constant string


On Wed, Jan 4, 2012 at 1:07 PM, Arun Vishwanathan <aaron.nar...@gmail.com>wrote:

> if instead of passing "hello" directly to function if we passed char array
> p then this would not show as an error right? and why is this so? is it due
> to the fact tat p array was not possibly allocated in the read only segment
> of memory and hence when passed it can be modified by function? so if const
> string is passed directly what causes the error?
>
>
> On Sat, Aug 27, 2011 at 11:28 AM, sagar pareek <sagarpar...@gmail.com>wrote:
>
>> @raj
>>
>> u already mentioned that if we write :-
>> char *p="hello";
>> p[0]='k'; // gives runtime error
>>
>>
>> so if we are passing arguments as
>>
>> modify(char a[],char *b)
>> {
>> .
>> .
>> }
>>
>> main()
>> {
>> .
>> .
>> modify("hello","hi");
>> .
>> .
>> }
>>
>>
>> then its actually
>> char arr[]="hello";
>> char *b="hi";
>>
>> so ofcourse now
>> b[0]='a'; // give u runtime error
>>
>> now u may be confuse about
>> arr[0]='a'; //gives runtime error
>>
>> here i would like to tell you that arr is char pointer not char array....
>> you can check by yourself in :-   http://www.ideone.com/EQrjj
>>
>> On Sat, Aug 27, 2011 at 10:39 PM, raj kumar <megamonste...@gmail.com>wrote:
>>
>>>
>>> "monsters are monsters"
>>>
>>>
>>>
>>> ---------- Forwarded message ----------
>>> From: raj kumar <megamonste...@gmail.com>
>>> Date: Sat, Aug 27, 2011 at 10:30 PM
>>> Subject: Re: [algogeeks] Re: String passing
>>> To: algogeeks@googlegroups.com
>>>
>>>
>>> can't understand what are you trying to say
>>>
>>> source
>>>
>>>  --
>>> You received this message because you are subscribed to the Google
>>> Groups "Algorithm Geeks" group.
>>> To post to this group, send email to algogeeks@googlegroups.com.
>>> To unsubscribe from this group, send email to
>>> algogeeks+unsubscr...@googlegroups.com.
>>> For more options, visit this group at
>>> http://groups.google.com/group/algogeeks?hl=en.
>>>
>>
>>
>>
>> --
>> **Regards
>> SAGAR PAREEK
>> COMPUTER SCIENCE AND ENGINEERING
>> NIT ALLAHABAD
>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to algogeeks@googlegroups.com.
>> To unsubscribe from this group, send email to
>> algogeeks+unsubscr...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
>
>
> --
>  "People often say that motivation doesn't last. Well, neither does
> bathing - that's why we recommend it daily."
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
Regards,
Rahul Patil

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to