unsigned int y = 3;
unsigned int *py = &y; // py contains the address of y
Serial.println (y);
Serial.println (*py); // *py is value to which py points
But
byte value = *((byte*) &y);
is something different. (byte*) &y means take the address of y (which is unsigned int) and treat it like it would be a byte - this is just a type-casting
. The outer *() means take the value to which this byte pointer points to. This would be the first byte of variable y which consists of two bytes (unsigned int in Arduino has two bytes).
indeed I should have made a note for this. (which I added now)
I should have used as a cryptic example
int y = *p * *++p;
(which should still have raised some eyebrows because the order in which the two operands of *p * *++p are evaluated is unspecified as the implementation may fetch *p first and then *++p, or do it in the reverse order. But in either case the behavior is still defined — you always end up multiplying the value pointed to by the original p with the value pointed to by the incremented p.)
I'm not alone in thinking that it's square brackets, curly braces, and parentheses, which are round. Maybe it's a regional thing. Separately, if you're going to cast, C++ casts -- several types, but "basic" Arduino code uses mostly two? -- are certainly more verbose, but they don't get lost in a sea of parentheses. There's also a specific type for pointers as a number,
USER is trained to USE some tools to do some work.
DEVELOPER is trained to DEVELOPE some applications to satisfy some needs.
And TEACHER is trained to TEACH and EXPLAIN different kinds of things.
Being DEVELOPER does not automatically mean to be TEACHER and vice versa
Also the question about PURPOSE allows for many different correct answers. Lika stating, that it is clear enough, what IRON is, but asking for its PURPOSE.
One may answer - to make nails to fasten wood together.
Lumberjack will say to make axes and saw to split wood.
Armorer will say to make armor to keep human flesh intact.
Chirurg will say to make scalpers to cut human flesh.
And all of them have valid answer.
But well, the purpose of pointers is to allove organisation of data and code which would be impossible or extremely hard without pointers - happy?
(I will leave this thread to the theory and discusions about developing and teaching skills)
One of many PURPOSES of pointers is to create dynamically groving data structures, which may be interconnected in many ways.
One of many PURPOSES of such structures is to contain compiled bytecode for FORTH language and allow its execution.
One of many PURPOSES of FORTH is to allow user interactively add more functions to existing programs without need to recompile and upload the whole code to Arduino for each and every minor change.
One of many PURPOSES of building programs interactively is to be able test PCB from inside as I add more and more periferials to my creation. From MCU and simple serial connector to full featured “home computer” with VGA output and PS/2 input and SD card for datastore.
One of many PURPOSES of this SBC (Single Board Computer) is to serve as graphical card to my project of 8bit computer build around HD6309 processor.
So one of many PURPOSES of pointers is to help me build retrocomputer.
The part around FORTH is interesting, as main variable - Instruction Pointer - is pointer in RAM, which points to RAM or FLESH to pointer, which points to RAM or FLESH to pointer, which points to FLASH where resides some function. (Which takes as parameter pointer to collection of data, where many of them are also pointers and some are something else) (That is, from where my previous example originated.)
Also while testing the PCB I developed some program tools for easier manipulating GPIO pins of Atmega chips.
And another result is game Snake, which fits on one screen of code and is written in the FORTH to - to demonstrate the hardware.
So PURPOSES of pointers (as purposes of iron) are plentifull
Now, you know how to create/form a pointer variable and you have seen one application of pointer in #28; where, pointer has been used to get the content of a memory location.
Veteran posters of this thread have described/explained the purposes of pointer. You can ask ChatGPT for more applications of pointer and study them. One example provided ChatGPT is given below:
Example-1: (Displaying on Serial Monitor of Arduino UNO the DC voltage of the signal pin of LM35 type analog temperature sensor)
int sensorPin = A4; // LM35's signal is connected with A0 (Ch-4 of ADC)
// Function that reads a sensor and stores result using a pointer
void readSensor(int *valuePtr)
{
int raw = analogRead(sensorPin); // read raw ADC value
*valuePtr = raw; // store result at pointer location
}
// Function that converts ADC value to voltage using pointers
void convertToVoltage(int *adcValue, float *voltPtr)
{
*voltPtr = (*adcValue) * (5.0 / 1023.0); // scale to 0–5.0V
}
void setup()
{
Serial.begin(9600);
}
void loop()
{
int adcValue;
float voltage;
// Use pointers to update variables indirectly
readSensor(&adcValue); // pass address of adcValue
convertToVoltage(&adcValue, &voltage); // pass addresses
Serial.print("ADC Value = ");
Serial.print(adcValue);
Serial.print(" | Voltage = ");
Serial.print(voltage);
Serial.println(" V");
delay(1000);
}
The VDT (DC Voltage at T degC temp) goess to Ch-4 of the internal ADC via A4-pin of the UNO Board. The VDT sgnal is digitized and the value is saved in variable raw which is stored in a 16-bit wide RAM space (as adcValue) whose base address is in pointer variable (valuePtr).
Example-2:
The above sketch can be written as follows without using pointer.
void setup()
{
Serial.begin(9600);
}
void loop()
{
unsigned int adcValue = analogRead(A0); //adcValue = 0 to 1023
float sensorVolt = (5.0/1023.0)*adcValue; //ADC Full Scale = 5.0 V
Serial.print("Voltage at Signal Pin of Temp Sensor: ");
Serial.println(sensorVolt, 1); //1-digit after decimal point
delay(1000);
}
Do you see any advantage of using pointer in ChatGPT's sketch of Example-1? ChatGPT's sketch shows how parameters are exchanged between functions using pointer variable.
Example-3: Moving 8-byte data from one memory space to another memory space using pointer variable.
... it is left as an exercise for @galacticobmg.
C++ pointers help manage memory directly, and managing memory directly helps build highly efficient software. Highly efficient software helps enable complex simulations that would otherwise be impossible. Complex simulations help scientists model the behavior of galaxies, black holes, and dark matter. Modeling galaxies and dark matter helps us understand the fundamental laws of physics. Understanding the laws of physics helps us devise new technologies and energy sources. New technologies and energy sources help humanity tackle existential threats. Tackling existential threats helps us preserve civilization. Preserving civilization helps us ensure the survival of life. Ensuring the survival of life helps us protect the universe’s conscious witnesses. Protecting conscious witnesses of the universe means saving the universe. Therefore, pointers save the universe.
Looking at this thread and the related topics below this discussion (Verifying that My Understanding of Pointers Is Correct, Is it necessary to use pointers in Arduino C?, "Pointers make my head spin" etc ..., Seeking better explanation of Pointers * &), I get the feeling that we need a special category just for pointers.
Anyway - in *p * *++p, p is read in *p and modified in ++p with no sequence between them, so it falls exactly into that UB category. The compiler is free to evaluate operands in any order, and because there’s no sequencing, the standard classifies this as undefined behavior. On some compilers it may appear to “work,” but it is not guaranteed by the language.
…and I am tooooo dinky to think that I can save the world if I know about pointers but in my small personal environment I can be proud to make my DIY clock working well without even knowing what an * can be used in a sketch for more than a multiplication.
very and I mean very first time in my life that I have seen such a good explanation for this task. @GolamMostafa are you a USER, DEVELOPER or TEACHER ? …or perhaps all of them ?