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 / Pointer to a string

Author
Message
Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 17th Jun 2008 18:21
I'm trying to create a pointer to a string, so that I can pass it to a function and that function can do a load of funky things to that string. Note that I don't want to simply pass a copy of a string to the function. I've tried various things with no luck, any ideas?

Here is some code that should demonstrate what I am trying to do:
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 17th Jun 2008 18:30
I think this will work. Hope it helps.

Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 17th Jun 2008 18:35
Why convert it to a string at all?

Don't you just hate that Zotoaster guy?
Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 17th Jun 2008 18:43
Strings have saved me a lot of time with all of the functions that the library contains. I have amazed myself, and managed to get it to work whilst writing code to further explain what I am trying to do. I wanted to be able to direct a function to a string and let it perform operations on that string. The changes must not be lost once the function ends as would be the case with Mahoney's code. This now works fine:
Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 17th Jun 2008 19:01
I've got another problem now: how do I gain access to the string operations using a pointer to a string I tried (as a test operation):

*sText.erase(0,5);


Full code:
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 17th Jun 2008 19:48 Edited at: 17th Jun 2008 19:49
Instead of *sText.erase(0,5), use this syntax:



-> is the syntax for pointers instead of a .
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 17th Jun 2008 19:57
char* strings have to be pointers because they point to somewhere in memory (surprised?). They act kind of like an array.

std::strings don't need to be pointers. Simply doing "string MyString;" works fine.

Don't you just hate that Zotoaster guy?

Login to post a reply

Server time is: 2024-11-20 13:33:28
Your offset time is: 2024-11-20 13:33:28