View unanswered posts | View active topics It is currently Tue Aug 04, 2020 1:44 am

 Page 1 of 1 [ 5 posts ]
 Print view Previous topic | Next topic
What are the use of pointers?
Author Message
Senior Roboticist

Joined: Wed Sep 28, 2011 10:13 pm
Posts: 630
Location: If I told you, I'd have to kill you.
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
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
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:
 Code:void GetSensorData(tSensors sensorID, int *pData){    *pData = SensorValue[sensorID];}void GetSensorData2(tSensors sensorID, int data){    data = SensorValue[sensorID];}task main(){    int distance = 0;    GetSensorData2(sensorSonar, distance);    nxtDisplayTextLine(0, "dist0=%d", distance);    GetSensorData(sensorSonar, &distance);    nxtDisplayTextLine(1, "dist1=%d", distance);}

Then you may say, if I want to return the sensor value, can't I just return it through the return statement?
 Code:int GetSensorData(tSensors sensorID){    return SensorValue[sensorID];}

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:
 Code:void GetAccelData(tSensors sensorID, int *xAccel, int *yAccel, int *zAccel){    *xAccel = something;    *yAccel = something:    *zAccel = something;}

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
Expert

Joined: Thu Sep 29, 2011 11:09 pm
Posts: 184
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
Expert

Joined: Tue Feb 28, 2012 3:10 pm
Posts: 197
Re: What are the use of pointers?
http://stackoverflow.com/questions/5748 ... iable-in-c

Use 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
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
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
Display posts from previous:  Sort by
 Page 1 of 1 [ 5 posts ]

#### Who is online

Users browsing this forum: No registered users and 1 guest

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning