Dyslexicbloke:
Can you help me understand this pointer issue a bit further ...
You seem to be saying that I should never increment/decrement pointers because I cant know what is where and cant therefore rely on the information I will get back.
This is because unless the variable is part of an array its location will chance even if its instance is never destroyed once its been created.Is that correct, It seems counter intuitive ... Am I missing something?
Also why memory allocated to a global variable change in the first place?
Pointers hold the memory location of a piece of data. If you have a pointer to a char, then it holds the address of the memory location where a char is stored. You can alter the value of the char and that is fine - the storage location does not change, the pointer does not change.
When you increment a decrement a pointer, you are changing it to refer to a memory location which is adjacent to the original location. The compiler promises to keep elements of an array next to each other, so it is legitimate to use pointer arithmetic to refer to adjacent elements of the array - as long as you avoid going past the end of the array. But if the thing pointed to is not within an array, you the programmer have no way of knowing what is stored next to the original storage location. In that case, incrementing/decrementing the pointer is wrong - you would be changing it to point to an arbitrary piece of memory that you have no reason to assume holds anything in particular.
Having said all that, you do not need to use pointer arithmetic at all to do what you're doing. You will have an array of structs, and each struct will contain a name and a pointer to a variable. You can access each element of the array using foo [ i ] notation so there is no need for pointer arithmetic, and when you come to dereference the pointer you will just use the '*' operator.
Will all your variables be of the same type? If not, your structure ought to hold multiple pointers (one for each type you need to support) of which only one would be non-null for a given item.