Archived

This topic is now archived and is closed to further replies.

boxdot3

Strings

Recommended Posts

I have looked through MSDN to try and find a function equivilent to VBs Mid function, but i wasnt able to find one for c or C++. What i need is something that i can call that will return what is in the string at a specified position. i.e. I have the string "Im a Newb" I want to be able to find the 4th character in the string, which in the case would be "a". Any ideas? Thanx for any help! Simon

Share this post


Link to post
Share on other sites
const will ensure the value won't change. This is especially important with pointers.

Even if you don't change the value the pointer points to, it's still possible that the pointer itself could be mangled accidently.

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/

[edited by - Chem0sh on June 8, 2002 11:54:33 AM]

[edited by - Chem0sh on June 8, 2002 11:55:45 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Chem0sh
Even if you don''t change the value the pointer points to, it''s still possible that the pointer itself could be mangled accidently.


There is a difference between pointer to const data and const pointer. For example:

  
// pointer to const data

const char *p = "text";
// this won''t compile

*p = ''T'';
// this is legal

p = "another text";
// const pointer

char * const p = "text";
// this is legal

*p = ''T'';
// and this won''t compile

p = "another text";
// const pointer to const data

const char * const p = "more text";

I was asking because the string literal might be put into constant memory by the compiler, and an attempt to modify it might cause access violation. I think I read something like "string literals should be const char *, but they are char * so that C code compiles" in Stroustrup but I can''t remember now.

Share this post


Link to post
Share on other sites