hostseries.blogg.se

Robotc vex tutorial
Robotc vex tutorial






When you look at what it looks like in memory, you can get a good idea of what’s going on: It is important that on the second line, the address arrPtr points to is one higher than in the first one. Please note that the address arrPtr may be different in your case. That should print out something like this: arr: 1, arrPtr: 656F6968, *arrPtr: 1 WriteDebugStreamLine("arr: %d, arrPtr: %p, *arrPtr: %d", arr, arrPtr, *arrPtr) ĪrrPtr++ // we're now pointing at arr

#ROBOTC VEX TUTORIAL CODE#

Consider the code below: // ptr tutorial example 3ĪrrPtr = &arr // arrPtr now points to the address of arr Now that you know how what pointers are, let’s have some fun with them. If you look at it from a memory perspective, it looks like this: The output should look like this: i: 10, iPtr: 656F6968, *iPtr: 10

robotc vex tutorial

WriteDebugStreamLine("i: %d, iPtr: %p, *iPtr: %d", i, iPtr, *iPtr) IPtr = &i // iPtr now points at the address of i Combining all this new-found knowledge, we get the following: // ptr tutorial example 2 But what if we want to see the value of this variable we’re pointing at? We can do that with the dereference operator, also a “*”. With a pointer, the rvalue is in fact the address of the variable we’re pointing at. Previously, we saw that the rvalue of a variable contains the actual value that we assigned. Fear not, they didn’t add this ability to get the address without being able to doing something practical with it as well. This isn’t very useful in itself, though. Should you want to access this address in ROBOTC, you can do so with the reference (&) operator. This chunk has an address, a number, much like a house. Getting to the point(er)Īs I mentioned earlier, when a variable is declared, the compiler assigns an appropriately sized chunk of memory to it. What happens when we increment i? Does it change j’s rvalue? In short, no, the two rvalues are completely separate entities.

robotc vex tutorial

However, what happens on the line “j = i”? Simple, the rvalue of i is copied and assigned to j’s rvalue. On the first assignment, i’s rvalue is given the value 10, on the second assignment, j’s rvalue is given the value 51. When looking at the memory locations and their contents after each operation, it would look something like this: The output of this program should look like this: i: 10, j: 51 WriteDebugStreamLine("i: %d, j: %d", i, j) Now consider this bit of code: // ptr tutorial example 1

robotc vex tutorial

As you can guess, the rvalue is the part of the right side of the assignment and lvalue is the part on the left side.

robotc vex tutorial

You see the memory blocks, the labels assigned to them, their contents and the addresses (the hex numbers under the blocks).Ī variable has two parts, an rvalue and an lvalue. An int is 4 bytes large, a float is also 4 bytes but a string (in the case of ROBOTC) is usually 20 bytes large. In effect, when you declare a variable, the compiler puts aside an appropriately sized amount of memory and gives it a symbolic name, like i or f or baz. Up until now, ROBOTC only supported normal variables in other words, a variable, be it an int, float or anything else, only had a value, like 712, 0.383 or “howdy”. This tutorial is the first of a couple that will show you the kinds of things you’ll be able to do with pointers. Hurray, ROBOTC has support for pointers! “That’s nice,” I hear you say, “but what are they?” That’s a good question.






Robotc vex tutorial