Making assumptions about how variables will be stored in memory is a very dubious practice.
I thought as much which is why I asked ... However it appears to be working, all be it with a simple test.
I purposely defined other stuff between the instances that I am iterating through in an attempt to break it.
If anyone thinks that global variables will move in memory for some reason I would be interested to understand.
Does it matter? You still need to maintain a pointer to it, or you won't find it.
What's wrong with the first one as, for want of a better description, a base address?
Incrementing a pointer only makes sense when the pointer points to an array of items. If you create discrete instances, even if they are adjacent in memory, you can't iterate through the instances by incrementing a pointer.
It would appear not ... output below code!
typedef struct{ // Definition of Type structure for xVar
String Name;
float Value;
}xVar;
xVar BatVolts; // New instance of xVar, first instance
int JustJunk1 = 10; // Junk to ensure that the two instances of xVar are not together, at least logically, who knows what the compiler does.
int JustJunk2 = 32000;
xVar AcVolts; // New instance of xVar
xVar *PtrxVar = &BatVolts; // pointer to BatVolts, the first instance of xVar
void setup()
{
pinMode(13,OUTPUT);
digitalWrite(13,HIGH);
Serial.begin(9600);
Serial.println("");
BatVolts.Name = "BatVolts"; // Stick some values in so I have something to print
BatVolts.Value = 12.5;
AcVolts.Name = "AcVolts";
AcVolts.Value = 236;
Serial.print(BatVolts.Name); // Print BatVolts values just to check
Serial.print(" >> ");
Serial.println(BatVolts.Value);
Serial.println("");
Serial.print(PtrxVar->Name); // Print BatVolts values again, this time using the pointer as a reference
Serial.print(" >> ");
Serial.println(PtrxVar->Value);
Serial.println("");
PtrxVar = PtrxVar++; // Increment the pointer, +1 works just as well.
Serial.print(PtrxVar->Name); // Print values using the pointer as a reference, this should be AcVolts, and it is
Serial.print(" >> ");
Serial.println(PtrxVar->Value);
Serial.println("");
PtrxVar--; // Decrement the pointer, -1 works just as well.
Serial.print(PtrxVar->Name); // Print values using the pointer as a reference, this should be BatVolts again
Serial.print(" >> ");
Serial.println(PtrxVar->Value);
}
void loop()
{
digitalWrite(13,!digitalRead(13));
delay(250);
}
The result ...
BatVolts >> 12.50
BatVolts >> 12.50
AcVolts >> 236.00
BatVolts >> 12.50
I am new at this and know very little,
if this is likely to break when it is just part of a bigger program, as opposed to an isolated test, that is something I need to know about but as you can see it works.
Please feel free to pull it to pieces so I can learn ...
Comments earlier, even the negative ones, have pointed me in this direction and ultimately got this working.
[ I will not be using the String object in the final version but I thought if it worked with that then it would probably work with anything. ]