Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / char* get character

Author
Message
AlexI
19
Years of Service
User Offline
Joined: 31st Dec 2004
Location: UK
Posted: 20th Mar 2009 23:56 Edited at: 20th Mar 2009 23:58
Hi,
What the quickest way to get 1 character from char* string if you know the letter number? and return it as another char* ?

So I need:

char* test="random"

// get character 3

char* character="n"


I know if it was just a char I could just do charstring[3] but this doesnt work with char*. I know there's dbLeft and dbRight but I want to dont want to use this.

Thanks,
Alex

dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 21st Mar 2009 00:16 Edited at: 21st Mar 2009 00:16
Just make an array with a length of 2, copy the required char in, and zero the last index(the code below will do the latter two in reverse).

i.e.:



Alternatively you could use std::string:



AlexI
19
Years of Service
User Offline
Joined: 31st Dec 2004
Location: UK
Posted: 21st Mar 2009 01:01 Edited at: 21st Mar 2009 01:01
I am using char* not char. I need to get one letter from a char* of which I know its letter number.

dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 21st Mar 2009 01:48
And if you look, my code does exactly this.

AlexI
19
Years of Service
User Offline
Joined: 31st Dec 2004
Location: UK
Posted: 21st Mar 2009 13:29
I just used this code in the end:



Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 24th Mar 2009 00:00
If you're asking that the function return a char pointer to the character in an array when you have a char pointer to the first char and the offset then the char * you're looking for is simply the original char pointer plus the offset. That's the way pointer arithmetic works.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Benjamin
22
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 24th Mar 2009 00:10 Edited at: 24th Mar 2009 00:11
I don't know why you would ever need such a function, these kind of operations are fundamental:



If you want to take a char and output it, dark coder already showed how to do this.

http://www.cplusplus.com/doc/tutorial/

Login to post a reply

Server time is: 2024-11-25 16:46:49
Your offset time is: 2024-11-25 16:46:49