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 18 of 18 Topic 26062 of 27670
Post > Topic >>

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

by =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= <toe@[EMAIL PROTECTED] > May 6, 2008 at 07:39 AM

On May 6, 2:50=A0pm, Ri**** <postri...@[EMAIL PROTECTED]
> wrote:

> void foo(char *s,int *x){
>
> int *f=3Dx;
> char *k=3Ds;
> *f=3D6;
> *k=3D'a';
>
> }
>
> int main(){
>
> int a[5]=3D{1,2,3,4,5};
> char *s=3D"hello world";


There's a quirk in C. The type of a string literal is a non-const
array of char's, even though you're not allowed to modify them.
Stupid, I know.

In your function above which attempts to modify a string literal, you
get a thing known as "undefined behaviour". In the C Standard,
"undefined behaviour" is a cop-out whereby the Standard says "if you
don't follow the rules, then anything can happen, you program may
work, it may not work, I don't care".

In your particular case, on your particular system, the behaviour
which is not defined by the Standard has resulted in nothing
happening. Try changing the code as follows:

    char *s =3D "hello world";

to:

    char s[] =3D "hello world";

Now you should see that your function can change it.
 




 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 Fri Oct 10 21:58:23 CDT 2008.