Re: Problem using string pointer

From: David Webber (dave_at_musical.demon.co.uk)
Date: 03/10/04


Date: Wed, 10 Mar 2004 08:15:11 -0000


"kailasam" <kailasam.pk@in.bosch.com> wrote in message
news:c2mhir$ksp$1@ns1.fe.internet.bosch.com...

> METHOD 1 :
> char *str = " ADGFR";
> *str = 'c';

One learns not to do this. The actual data " ADGFR" is just a
constant stored somewhere or other and str is a pointer (put on the
stack) which points to it - wherever it is. I am not sure that the
C or C++ languge specification says where " ADGFR" should be stored,
or what you should be allowed to do with the data buffer. If you
really must use this then

const char *str = " ADGFR";

is much better.

> METHOD2:
> char str[] = " ADGFR";
> *str = 'c';

This is completely different! My understanding is that it allocates
7 bytes on the stack for str[] and copies " ADGFR" into it. The
data on the stack is your property and so you can manipulate str[]
as you would any old 7 bytes of stack.

Dave

-- 
David Webber
Author MOZART the music processor for Windows -
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm


Relevant Pages

  • Re: Comments please
    ... > printdate(str); ... > Segmentation fault ... not on the stack. ... this behavior - its just a curious side effect of compiler architecture ...
    (comp.lang.c)
  • Re: whether this is valid
    ... located in data segment which is read only. ... And your *a is a pointer (located in your stack that means it be able ... This variable pointed to address that "str" located. ... you in stack and copy "str" from data segment to that memory. ...
    (comp.lang.c)
  • Unused stack space
    ... In a C module that I have compiled, it seems to generate object files ... which waste stack space. ... (to pass the str parameter to printf), ...
    (comp.os.linux.development.apps)
  • Problem using string pointer
    ... char *str = " ADGFR"; ... The above simple code gives runtime error. ... (the memory could not be written) ...
    (microsoft.public.vc.language)
  • Re: if derived class adds an argument to ctor, does that need to carry up to the base classes?
    ... > below (thanks to Ken Kolda's earlier newsgroup assistance.) If a client ... > stack until B(string str) handles it. ...
    (microsoft.public.dotnet.languages.csharp)