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.

Code Snippets / [DBP] Returning mutiple variables from functions with pointers

Author
Message
Toz
17
Years of Service
User Offline
Joined: 14th Oct 2007
Location: The Netherlands
Posted: 14th Oct 2007 19:56
Hello everyone I just joined the forum today even though I'm using DBPro for over 3 years now. The reason that I joined is that I could post this snippet. It shows a way to return multiple variables from a function with the use of pointers. I found out how to do this when I was learning how to use functions in C. In C you can use the "&"-symbol to return the adress of a variable. Unfortenately you can't do this in DBPro, so I had to use
the make memory command since that does return a adress in memory. How the rest of the code works is explained in the code snippet.



I hope that a lott of you will find this code usefull.

Don't read this, read the above.
Alsan
19
Years of Service
User Offline
Joined: 20th May 2005
Location: Germany - Nähe Kassel
Posted: 15th Oct 2007 15:26
Though the title is a bit confusing I finally understood how to use the "make memory" command and pointers. Thanks.

Visit my Forum [German]: www.jaf-entertainment.de
Toz
17
Years of Service
User Offline
Joined: 14th Oct 2007
Location: The Netherlands
Posted: 15th Oct 2007 16:24
Yeah maybe I've should have named the title updating mutiple variables in functions with pointers. And off course you can also do this with global variables. I just wanted to show a nice example of what you can do with pointers.

Don't read this, read the above.
Mr Tank
21
Years of Service
User Offline
Joined: 25th Nov 2002
Location: United Kingdom
Posted: 15th Oct 2007 19:37
well now i know what pointers are.
What is the point in pointers though? Why not just declare arrays?

SUPER BADASS SPACESHIP X: WEBSITE
FORUM TOPIC
Toz
17
Years of Service
User Offline
Joined: 14th Oct 2007
Location: The Netherlands
Posted: 15th Oct 2007 20:50
Why you should use pointers? Well that's really a question of with what kind of data manupulation your more comfatrable with. I don't use pointers much myself but they do seem to be handy with image manupulation. A while ago some other DBPro user named Azrael posted a great example showing how you could use pointers that way.

Here is the link:http://forum.thegamecreators.com/?m=forum_view&t=94598&b=6

Don't read this, read the above.
Math89
20
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 18th Oct 2007 22:32 Edited at: 18th Oct 2007 22:35
You can return all the variables into the same memory space instead of creating 4 bytes for each one.


Pointers are really usefull, with a bit of practice, you can can make a list system that allows you to put a list into UDT (wich is in my opinion really usefull).
Hobgoblin Lord
19
Years of Service
User Offline
Joined: 29th Oct 2005
Location: Fall River, MA USA
Posted: 4th Nov 2007 11:28
This is also not quite the same as making a and b global. If those are the only two variables you are dealing with then sure this would work fine. but what if you have hundreds and want to be able to switch any of them with one function? as demonstrated below, the function is quite useful as it allows you to swap any combination all in one function.



Login to post a reply

Server time is: 2024-11-22 12:53:41
Your offset time is: 2024-11-22 12:53:41