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 / Return 2 values using Pointers

Author
Message
Dave J
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Feb 2003
Location: Secret Military Pub, Down Under
Posted: 25th Mar 2004 12:31
Just thought I'd share this piece of code because a lot of people are unfamiliar with how to use pointers and what their capabilities are. This is one example. You pass two variables to the function and that function will then be able to modify these two values so you can essentially return two (or more if you wanted) values from a function rather then just one. This particular example swaps the two values with eachother.



Hope it's useful to someone.


"Computers are useless they can only give you answers."
Mussi
21
Years of Service
User Offline
Joined: 27th Jan 2003
Location: Netherlands
Posted: 25th Mar 2004 21:12
did not know pointers exists in DBP , only knew about the memblock ptr() command, now I know



Specs: AMD Athlon 1800, 256 DDRRam 266mhz, 80GB HD 7200rmp U133, Geforce 4 Ti4400 128mb
Dave J
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Feb 2003
Location: Secret Military Pub, Down Under
Posted: 26th Mar 2004 07:36
lol yeah, I only just learned of their existance in DBP recently as well. Hence why I decided to see if I could do that little C++ trick in DBP.


"Computers are useless they can only give you answers."
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 26th Mar 2004 09:09
Woh, I never knew this. Thx for pointing this out.

"eureka" - Archimedes
ifman1
20
Years of Service
User Offline
Joined: 3rd Jul 2003
Location:
Posted: 30th Mar 2004 23:57
wow, can you have a pointer to an array? Or pass an array in and out of a function like you can in C++?
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 31st Mar 2004 12:21
arrays are global anyway, so why would you have too?

"eureka" - Archimedes
Dave J
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Feb 2003
Location: Secret Military Pub, Down Under
Posted: 31st Mar 2004 15:08
Quote: "wow, can you have a pointer to an array? Or pass an array in and out of a function like you can in C++? "


That's a good question. The problem I see with DBP is that you access memory like this:

PointName = Gets/Assigns Memory Address
*PointName = Gets/Assigns Value in Memory

And if it was a normal variable:

PointName = Gets/Assigns Value in Memory

So you can see it will have some clashes if it's not explicitly defined as a pointer (which is done by placing a memory address in it's value). In C++, it is:

PointName = Assigns Memory Address
&PointName = Gets Memory Address
*PointName = Gets/Assigns Value in Memory

And those will work with any variable basically, even arrays. This is why you can use pointers with arrays so easily. It's just:

Pointer = &ArrayName

Unfortunately, you can't do this in DBP because obviously it will just assign the value in ArrayName into the Pointer value. So, I'd have to say no, you can't do it with arrays (unfortunately), if someone has another idea though, I'd love to hear it.


"Computers are useless they can only give you answers."
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 1st Apr 2004 14:25
You can have arrays pointer in DBPro with my array plug-in.

There are good reasons for doing this. If you have several arrays and need to perform the same operation on each you would currently need to write the code twice. With my plugin you can pass the array pointer to a single piece of code instead.

The only restriction with my plugin is that you cannot enlarge the array when you are using the pointer.

*** Coming soon - Network Plug-in - Check my site for info ***
For free Plug-ins, source and the Interface library for Visual C++ 6, .NET and now for Dev-C++ http://www.matrix1.demon.co.uk
ifman1
20
Years of Service
User Offline
Joined: 3rd Jul 2003
Location:
Posted: 2nd Apr 2004 16:39
aaahhh, sacrifice the dynamic array for allocated memory! Durn the laws! Durn them all.

Login to post a reply

Server time is: 2024-05-13 03:07:24
Your offset time is: 2024-05-13 03:07:24