Re: strings

From: .:'DevarajA':. (no_at_spam.com)
Date: 08/22/04


Date: Sun, 22 Aug 2004 09:45:12 GMT


"James Dennett" <jdennett@acm.org> wrote:
> .:'DevarajA':. wrote:
>
> > char *msg = "a string"; is there something wrong with this?
>
> Yes; it is not "const correct", in that you have set a
> char* to point at a const char.
>
> If after the declaration above you executed
>
> *msg = 'b';
>
> then you would have "undefined behavior"
>

sorry, maybe i've not understood...
If I executed it..., but I just need to have a string which can be read when
needed. The program doesn't modify it. In this case would it be correct?
I've also been told that char *msg = "aaa" writes the string aaa in a
random memory location, sometimes overwriting important data. Is it true?



Relevant Pages

  • Re: MFC dll and exe porting to 64-bit
    ... Why the obsolete 'char *' instead of the proper LPTSTR? ... We are no longer programming 16-bit windows in an 8-bit character world, ... string arguments should probably be LPCTSTR, that is, const parameters. ... declaration of the function itself, in context (meaning show the surrounding class ...
    (microsoft.public.vc.mfc)
  • Re: Combo Box String to char*
    ... Well, the real problem is most likely that the function should have been a const char *, ... or better still, an LPCTSTR parameter. ... number of characters in the string; if you give a longer value, ...
    (microsoft.public.vc.mfc)
  • Re: How to add thousand separators
    ... First, this code is obsolete as written, because char is a dead data type and should not ... Note that both of these should be stored as string resources since they might need to be ... 18 digits for any reason. ... you have made a VERY SERIOUS DESIGN ERROR. ...
    (microsoft.public.vc.mfc)
  • Re: what is the best way of passing floats into a string
    ... I do not null-terminate as snprintf takes care of this (according to ... But the easiest way to determine the size needed to format a number, ... int length_of_representation(double n,const char* format){ ... I get a nice result of -10.000000 in my char * string. ...
    (comp.unix.programmer)
  • Re: Converting BSTR to char*
    ... First, you should realize that a BSTR is a wide character string, whereas char* is a narrow string. ... This actually maintains both a narrow and wide string internally, and has cast operators for both const char* and const wchar_t*. ...
    (microsoft.public.vc.language)