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


|