Archived

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

boxdot3

Strings

Recommended Posts

boxdot3    242
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
IndirectX    122
quote:
Original post by Null and Void
char *Str = "Test";


I''m not trying to pick on you or something, but shouldn''t it be

const char *Str = "Test";

Share this post


Link to post
Share on other sites
Null and Void    1088
quote:
Original post by IndirectX
I''m not trying to pick on you or something, but shouldn''t it be
const char *Str = "Test";

I thought about it, but decided to keep it simple. I had const on there, but I erased it before posting.

Share this post


Link to post
Share on other sites
Jason Zelos    211
If you are after substrings (like VB mid), the last post contains some example C code:

http://www.gamedev.net/community/forums/topic.asp?topic_id=95233

,Jay

Share this post


Link to post
Share on other sites
Pipo DeClown    804
me sa newB,
whats the differents between const and no const?

If you don''t change the values in it...

_________________________________________________________________________
Can someone be nice and help me on my way to be the next Hideo Kojima?
Thought So...

Share this post


Link to post
Share on other sites
Chem0sh    122
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
IndirectX    122
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