Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: Pointer que...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 14 of 18 Topic 26080 of 26955
Post > Topic >>

Re: Pointer question...last for the day? :-)

by roberson@[EMAIL PROTECTED] (Walter Roberson) May 6, 2008 at 02:32 PM

In article
<349e535a-58f6-4cb9-bbdd-b528c60539ea@[EMAIL PROTECTED]
>,
Ri****  <postri****@[EMAIL PROTECTED]
> wrote:

>void foo(char *s,int *x){
>int *f=x;
>char *k=s;
>*f=6;
>*k='a';
>}

>int main(){
>int a[5]={1,2,3,4,5};
>char *s="hello world";
>foo(s,a);
>printf("%s %d",s,a[0]);
>return 0;
>}

char *s="hello world"; sets the pointer s to be a pointer to a
storage block with program lifetime, and with implementation-defined
semantics as to whether the block is writable or not.

char *s="hello world"; does NOT figure out how big the literal is,
create an array that big, and copy the contents of the literal in:
it sets the pointer to a memory block that already exists.

When you attempt your *k='a' in foo(), you are attempting to write
into the block of memory that is not certain to be writable at all.
If the block doesn't change as a result then the only fault was in
-trying- to change it. And if the block -did- change as a result,
then if you had had any other occurances of "hello world" as a string
literal in your code, those other occurances might have changed as well.

By the way, you did not output a \n at the end of your output.
C doesn't promise that the last line of output will appear at all in that
case. Always ensure you have a terminal \n when outputting to a text
stream.
-- 
  "No sincere artist was ever completely satisfied with his labour."
                                              -- Walter J. Phillips
 




 18 Posts in Topic:
Pointer question...last for the day? :-)
mdh <mdeh@[EMAIL PROTE  2008-05-05 19:52:37 
Re: Pointer question...last for the day? :-)
"Dann Corbit" &  2008-05-05 19:59:26 
Re: Pointer question...last for the day? :-)
Robert Gamble <rgamble  2008-05-05 20:02:07 
Re: Pointer question...last for the day? :-)
CBFalconer <cbfalconer  2008-05-05 23:11:41 
Re: Pointer question...last for the day? :-)
mdh <mdeh@[EMAIL PROTE  2008-05-05 20:06:09 
Re: Pointer question...last for the day? :-)
"Dann Corbit" &  2008-05-05 20:17:38 
Re: Pointer question...last for the day? :-)
rlb@[EMAIL PROTECTED] (R  2008-05-07 08:03:07 
Re: Pointer question...last for the day? :-)
Robert Gamble <rgamble  2008-05-05 20:26:37 
Re: Pointer question...last for the day? :-)
mdh <mdeh@[EMAIL PROTE  2008-05-05 20:31:51 
Re: Pointer question...last for the day? :-)
mdh <mdeh@[EMAIL PROTE  2008-05-05 20:40:28 
Re: Pointer question...last for the day? :-)
Peter Nilsson <airia@[  2008-05-05 21:18:54 
Re: Pointer question...last for the day? :-)
James Dow Allen <jdall  2008-05-05 23:48:32 
Re: Pointer question...last for the day? :-)
Rishi <postrishi@[EMAI  2008-05-06 06:50:30 
Re: Pointer question...last for the day? :-)
roberson@[EMAIL PROTECTED  2008-05-06 14:32:29 
Re: Pointer question...last for the day? :-)
Lowell Gilbert <lgusen  2008-05-06 11:47:01 
Re: Pointer question...last for the day? :-)
=?ISO-8859-1?Q?Tom=E1s_=D  2008-05-06 07:29:11 
Re: Pointer question...last for the day? :-)
Keith Thompson <kst-u@  2008-05-06 11:48:10 
Re: Pointer question...last for the day? :-)
=?ISO-8859-1?Q?Tom=E1s_=D  2008-05-06 07:39:42 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 2:32:34 CDT 2008.