View unanswered posts | View active topics It is currently Fri Oct 24, 2014 3:23 am






Reply to topic  [ 5 posts ] 
What are the use of pointers? 
Author Message
Senior Roboticist
User avatar

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

_________________
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
Profile WWW
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post 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
Profile
Expert

Joined: Thu Sep 29, 2011 11:09 pm
Posts: 184
Location: Michigan USA
Post 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
Profile WWW
Expert

Joined: Tue Feb 28, 2012 3:10 pm
Posts: 195
Post 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
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post 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
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users and 2 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

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.