|
Page 1 of 1
|
[ 5 posts ] |
|
What are the use of pointers?
| Author |
Message |
|
NeXT-Generation
Senior Roboticist
Joined: Wed Sep 28, 2011 10:13 pm Posts: 528 Location: Totally not spying on Hassenplug to see what he has for the Brickworld Chicago 2013 sumo contest.
|
 What are the use of pointers?
Yeah, I know what they do. I just don't really see why you would need to know where a variable is in the memory. What exactly is the point? 
_________________A.K.A. inxt-generation Self-proclaimed genius, and future world dominator. My Brickshelf Folder"Don't they teach recreational mathematics anymore?" - The Tenth Doctor Bow down to Nikola Tesla, King of the Geek Gods.
|
| Mon Sep 24, 2012 9:48 pm |
|
 |
|
MHTS
Guru
Joined: Sun Nov 15, 2009 5:46 am Posts: 1023
|
 Re: What are the use of pointers?
There are many uses of pointers. The simplest one is to pass the pointer of a variable into the function so that the function can change the content of the variable. In a function, if you pass a variable to a function (by value) and not its pointer (by reference), the function can change the content of the variable but it is a copy on the stack and it doesn't affect the real variable. For example: Then you may say, if I want to return the sensor value, can't I just return it through the return statement? Yes you can but what if the sensor has more than one data value that needs to be returned. For example, an accelerometer that has 3 axes. So you want to do this: It is also useful to be able to pass an array into a function which is also a pointer to the first element of the array, or passing a structure to a function by its pointer etc. There are many more uses. Just give you another example of using pointers:  |  |  |  | Code: void PrintDaysInMonth(string *names, int *days, int month) { nxtDisplayTextLine(0, "%s = %d", names[month], days[month]); }
task main() { int daysInMonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; string monthNames[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
PrintDaysInMonth(monthNames, daysInMonth, 1); } |  |  |  |  |
Last edited by MHTS on Mon Sep 24, 2012 10:42 pm, edited 2 times in total.
|
| Mon Sep 24, 2012 10:18 pm |
|
 |
|
mattallen37
Expert
Joined: Thu Sep 29, 2011 11:09 pm Posts: 158 Location: Michigan USA
|
 Re: What are the use of pointers?
Other than passing parameters by reference, the main thing I use pointers for is dealing with variables created using dynamic memory allocation, but apparently ROBOTC doesn't support it at this time.
_________________ Matt
|
| Mon Sep 24, 2012 10:38 pm |
|
 |
|
Spiked3
Expert
Joined: Tue Feb 28, 2012 3:10 pm Posts: 195
|
 Re: What are the use of pointers?
http://stackoverflow.com/questions/5748 ... iable-in-cUse references when you can, not pointers. Use pointers when someone else's code requires it. maybe if robotC had an "unsafe" keyword like C# it would encourage people to not use pointers.
_________________Mike aka Spiked3 http://www.spiked3.com
|
| Mon Sep 24, 2012 11:50 pm |
|
 |
|
MHTS
Guru
Joined: Sun Nov 15, 2009 5:46 am Posts: 1023
|
 Re: What are the use of pointers?
What Mike's talking about is the difference between pointer (e.g. int *) and reference (int &). Implementation wise, they are really both pointers but the syntax for reference does not allow "pointer manipulation". Therefore, it is considered safer than pointers. Pointer variables allow you to assign random values to it, so it can point anywhere in memory and allowing it to trash memory that it's not supposed to, whereas once a reference is bound to an object, it is fixed and cannot point to anything else.
|
| Tue Sep 25, 2012 12:11 am |
|
|
|
Page 1 of 1
|
[ 5 posts ] |
|
Who is online |
Users browsing this forum: capnrmorgan and 7 guests |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot post attachments in this forum
|
|