ROBOTC.net forums
http://www.robotc.net/forums/

What are the use of pointers?
http://www.robotc.net/forums/viewtopic.php?f=1&t=4889
Page 1 of 1

Author:  NeXT-Generation [ Mon Sep 24, 2012 9:48 pm ]
Post subject:  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:

Author:  MHTS [ Mon Sep 24, 2012 10:18 pm ]
Post subject:  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);
}

Author:  mattallen37 [ Mon Sep 24, 2012 10:38 pm ]
Post subject:  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.

Author:  Spiked3 [ Mon Sep 24, 2012 11:50 pm ]
Post subject:  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.

Author:  MHTS [ Tue Sep 25, 2012 12:11 am ]
Post subject:  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.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/