Welcome! In this series of short videos, we will look at topics common to both the PowerBasic console and Windows Compilers. Today we will look again at using pointers within your applications. Today we're going to be looking at pointers in PowerBasic. Pointers are variables that hold a 32-bit address of code or data located somewhere else in memory. It's called a pointer because it literally points to that location. I'm going to create a small application today that will demonstrate the use of pointers, what they are and how you can use them. So let's have a look at our code. Let's assume for this application that we're going to be storing some data on stars. We want to store a number of different pieces of information on each star. As the stars exist in a three-dimensional space, we want to store the x, the y and the z coordinates. Plus we'll also want to store the star's mass. There are a number of ways of actually storing these kinds of information, but today we're going to be using a user-defined type. This user-defined type we're going to call stars, udt stars. We're prefixing the name with udt to indicate that it is indeed a user-defined type. The four elements within this type are all single-type variables. Single-type variables are single-precision floating-point numbers, or to put it more simply, single-precision. These are very versatile. The values stored within one of these variables can contain decimal points. This single-precision representation comes as close as it can in six digits. So if your number has six digits within it, you can store it within this type of variable. This includes decimal places. For the purposes of what we're doing today, this will work out just fine. So now that we have our user-defined type set up at the beginning of the application, within any of the functions or subroutines, we can declare a local variable as that user-defined type. Then we can populate it. So here we are declaring a local variable called uStar. And we're declaring that as udt stars. So our local variable uStar, is now a user-defined type containing the four elements we declared at the beginning of the application. We can now populate these quite easily. I'm using the prefix command here to make the code look a little tidier. All the prefix command does is it takes the string that's in the double quotes and it starts each following line until end prefix with those values. So it's uStar. sngX equals and then the value for the first line. And it does the same for the next three lines. What we're doing here is we're setting the value of our x coordinate of the star to be a random number between 1 and 1000, and we're adding on another random number. That's a number between 0 and 1. So this will populate our user-defined type with the values randomly. If we're using random numbers, we'll want to set the random number generator to a random seed. We can do this at the beginning of our application, by using the randomise command with a seed number. To make this fairly unique, we're going to use the timer as our seed number. So now that we've populated this local variable, we'll now want to push the information out to the log so we can see what values have come out of it. So we're using the format command to turn these numbers into strings so we can use our logging command to send information out to the log. So if we try running our application now, it will display on screen a number of values for the x, the y, the z and the mass of our star. So we've now stored random information within our locally declared user-defined type. Of course, you can have arrays of user-defined types. So if we were looking to store information on a multitude of stars, we could use an array containing each element being this user-defined type. And this is where we can start to use pointers. But before we do the array, let's use pointers with what we've got – the single variable. So for the moment, let's comment out this code. And we will create some new code using a pointer. As we're going to be storing information on multiple stars and printing out information on multiple stars, I'm going to create two new functions – one to store the information on a star and one to output the information on a star. So we start off as we did before. We want to create a local variable as this user-defined type. So this is exactly the same line of code as we used before to declare our local variable as the user-defined type for stars. Next, we're going to prepare a pointer to be used for that. And here, our local variable is called "single star". And we're declaring that as the user-defined type, but we're following that with the word "pointer" – to say that this is going to be used as a pointer variable. And now that we've declared this pointer variable, we can now populate it. And we're populating this pointer variable by using the VARPRT function. This returns the 32-bit address of a variable. And in brackets, we have the name of the variable we're returning. So we're populating the 32-bit address into our pointer variable of where the information is stored for "U star". And as I said earlier, we're going to use two new functions – one to prepare the star to populate the values within the user-defined type, and one to print the details out. So we're calling the first function "prep star" and the printing function "print star". As you'll see from both of the calls to these functions, we're passing the pointer variable – the single star variable – to each of these functions. So let's now go and create the first of those two functions. So here is the skeleton of our new function – "prep star". As you will see in the brackets, this is where we normally put the name of the variable we're passing to this function. In this case, since we're passing a pointer, pointers have to be passed by value. So we have the "by value" word at the beginning of the list. Followed by the name of the variable we're bringing in. In this case, it's the pointer for "star". And we're declaring that as a user-defined type, which we declared at the beginning of our application. And at the very end, we have the word "pointer". This indicates this variable is in fact a pointer variable. So now that we have that set up, how do we populate the values using this pointer variable? So the lines of code to populate this value look remarkably similar to what we did before. The only difference is the prefix line. We're using an "@" sign in front of the "p" star variable. The "@" sign before the "p" indicates to Power Basic that we're using a pointer. And this will populate the values in our user-defined type quite nicely. So anytime you see the "@" sign in front of a variable, you know that it's a pointer that's being used. So now that we've populated the user-defined type using our "PrepStar" function, how can we print it out so the user can see it? So we'll now create our "PrintStar" function. So again here, you'll see we're taking exactly the same approach. We have "byval" before the name of the variable, and it's a user-defined type of stars. And again at the end we have the word "pointer". So we're passing exactly the same variable in exactly the same way to this printing function. And to print the details of our stars out to our log, we're taking exactly the same approach. We're using the "format" command to turn the value back into a string so we can use the fun log function. And the variable name is preceded with the "@" sign to indicate to Power Basic that it's a pointer variable. But the rest of the code looks exactly the same. We're still using the dot and the name of the element within the user-defined type. So now that we have that set up, our program should function in exactly the same way. So let's try running it and see how it goes. So there our program has run, and we have three coordinates for our star, and we have the star's mass. So we have successfully used a pointer with a locally defined user-defined type. So what are the advantages of using this pointer variable? Say for example we wanted to store multiple stars in an array of user-defined types. So let's dimension an array of 100 elements. And this is going to be an array of our user-defined type for stars. And we're going to step through this array using a For next loop. So as we've done before, we'll need a local variable to act as a pointer to our user-defined type, using exactly the same line of code as we did before, declaring PStar as a user-defined type of stars with the word "pointer" on the end to indicate that this is indeed a pointer variable. So let's now do the For next loop. So for each star, we're going to populate the pointer for the element of the array being processed. And we will populate the PStar variable again using var pointer, but this time pointing to the element of the array. And having got that value into PStar, we can now quite happily call our PrepStar function, exactly the same as we did before. So from the point of view of the PrepStar function, it's receiving a pointer. And it doesn't really matter whether you're passing the pointer to a single declared variable or an element of an array. It will handle both quite happily. So if we have now populated the value for each star, let's print some of those values out. Let's go for the first three stars. So again, we'll do a For next loop, and we will call our printStar function. So again, we're going for lngR equals 1 to 3. This will give us information on the first three stars. And we're populating the PStar pointer again using var pointer just as we did before, with the element of the array. And we're calling the printStar function with PStar as the variable being passed in. So let's run that code and see what we get. So there is our function running quite happily. And we have the information coming out of the array of stars just as we had the information coming out from the single variable. So as you can see, we're using the same functions to handle both single variable user-defined types and an array element, all using pointers. So to prove the information is coming out quite correctly, let's print out the third star without using pointers. To make sure the information is exactly the same. So here is the code to print out the values of the third star. We're setting the element number to be number 3. This is the third star. And we're printing out the x, y and z coordinates of that star, and also the mass of the star. So if we run this now, it should show exactly the same values coming out of the array as it is for this last piece of code. And there we have the value coming out of the array using the pointer functions and coming out directly without using pointers. And the values are exactly the same. So in summary, as you will see from this code, this is a simple way to use pointer, giving you a lot of flexibility in how you can actually use information which you're manipulating for users. Hopefully you'll find this code useful in your applications, but that's it for today. Thank you for watching.
