• Advertisement

Archived

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

Strings

This topic is 5737 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
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
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
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
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

  • Advertisement