Re: whether this is valid
From: Prawit Chaivong (arsenal_pui_at_hotmail.com)
Date: 06/29/04
- Next message: Eric Amick: "Re: usage of # in the following statement"
- Previous message: Barry Schwarz: "Re: C question: Returning string array from function"
- In reply to: madhur: "whether this is valid"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 28 Jun 2004 20:41:33 -0700
"madhur" <madhur@sdf.com> wrote in message news:<2k65g4F18kcpnU1@uni-berlin.de>...
> Hello
>
> char *a="str";
> strcpy(a,"abc");
"str" actually doesn't exist in your (function) stack. BUT It gonna be
located in data segment which is read only. (not 'a' itself, as you
said)
And your *a is a pointer (located in your stack that means it be able
to modified). This variable pointed to address that "str" located.
And then you tried 'strcpy(a,"abc");'. That means you tried to modify
memory that a pointed to. Do you remember I just mentioned that it is
read only data segment.
This code is not gonna work. Let change to char a[]= "str"; like the
other people in group said.
In this case compiler will allocate 4 bytes(Ignor alignment here) for
you in stack and copy "str" from data segment to that memory. And It
can be modify because it's located in stack segment.
Regards,
Prawit Chaivong.
>
> I have learnt that since "a" is a string literal, it might be allocated on
> read only memory on some implementations.
> Is it correct to modify a string literal using this way.
>
> Madhur Ahuja
> India
- Next message: Eric Amick: "Re: usage of # in the following statement"
- Previous message: Barry Schwarz: "Re: C question: Returning string array from function"
- In reply to: madhur: "whether this is valid"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|