The goal was to take a defined structure - one that is made up of both single and multi-byte variables - and use pointers to alter the contents of this structure. The user can use the index of the variable of interest rather than having to know where it is located in RAM. This makes the program more flexible and it also means that unless one rearranges the structure the user can be assured that the specific variables will always have the same index. It should be noted that the index is really the starting point of the variable within the real memory.
Here's a sample structure:
struct newstructure {
char firstvalue, //index 0
word secondvalue, //index 1
word thirdvalue, //index 3
char fourthvalue, //index 5
word fifthvalue //index 6
};
char firstvalue, //index 0
word secondvalue, //index 1
word thirdvalue, //index 3
char fourthvalue, //index 5
word fifthvalue //index 6
};
short *ptr;
ptr = &(newstructure.firstvalue);
The above would work great if all our variables were of type WORD however we also have CHAR variables which would cause us some offset problems. This offset would occur when we perform the following pointer math
*(ptr + index) = newvalue;
as the index would cause the pointer to move 2 bytes (word) per increment.
To ensure we can get to both types of variables we must satisfy our smallest variable so we rewrite the above.
char *ptr;
ptr = (char *)&(newstructure.firstvalue); In this case when we perform the pointer additions, the ptr will move a byte at a time which will not only allow us to modify CHAR based variables but also allow us to get to WORD variables that are odd aligned. With the above information in hand we can create two helper functions - one for CHAR values and one for SHORT values - that will look something like this.
bool changecharvalue(word offset, char value)
{
char *ptr;
ptr = (char*)&(structure.firstvariable);
*(ptr + offset) = value;
}
{
char *ptr;
ptr = (char*)&(structure.firstvariable);
*(ptr + offset) = value;
}
bool changewordvalue(word offset, word value)
{
{
char *ptr;
ptr = (char*)&(structure.firstvariable);
*(word *)((ptr + offset)) = value;
}
ptr = (char*)&(structure.firstvariable);
*(word *)((ptr + offset)) = value;
}
The changecharvalue function is pretty straightforward pointer addition plus dereferencing. The changewordvalue function however needs some explaining. Because we had to declare our pointer of type char, we must find a way to convert it to type word AFTER the pointer math has gotten it to point to our specific variable. We therefore dereference the pointer, get the address that it is now pointing too, cast that address as a pointer of type word and then dereference it again so that we can assign it the new value.
Although the whole pointer thing may look magical/complex/insane, pointers are probably one of the most efficient methods of writing software. Care must be taken to ensure that your points don't go errant, but once you understand pointers, your software will tend to get more compact and much more efficient.

No comments:
Post a Comment