| ??? 07/03/01 22:29 Read: times |
#13013 - RE: keil C: abstarct pointer |
Consider the standard 'C' library function malloc: void *malloc( unsigned int size ); This is perfectly standard ANSI 'C' - nothing Keil-specific here. As you know, malloc returns a pointer to the area of memory which it has allocated; now, it has no idea whether you want to put chars or ints or arrays or structures or whatever in this area - so it cannot return a char* or int* or any other specific pointer. It must therefore return as "generic" (or "Abstract" in Keil-speak) pointer which can refer to any arbitrary object Another example might be where you want to refer to an area of memory either as bytes, or words, or something else. The approaches of using shift-and-mask and unions have recently been discussed, but you could also use a Generic (or "Abstract" in Keil-speak) pointer; eg, void main( void )
{
void *AbstrPtr; // Abstract pointer
int Word1 = 0x55AA;
int Word2;
char Byte1, Byte2;
AbstrPtr = &Word1; // Point to Word1
Word2 = *(int *)AbstrPtr; // Dereference the pointer as an int
Byte1 = *(char *)AbstrPtr; // Dereference the pointer as a byte
Byte2 = *(char *)(AbstrPtr+1);// ... and access the next byte
}
When dereferencing an Abstract pointer, note that it's necessary to explicitly cast it so that the compiler knows what size object it's dealing with; eg, (int*) |
| Topic | Author | Date |
| keil C: abstarct pointer | 01/01/70 00:00 | |
| RE: keil C: abstarct pointer | 01/01/70 00:00 | |
| RE: keil C: abstarct pointer | 01/01/70 00:00 | |
| RE: keil C: abstarct pointer | 01/01/70 00:00 | |
| RE: keil C: abstarct pointer | 01/01/70 00:00 | |
| RE: keil C: abstarct pointer | 01/01/70 00:00 | |
| RE: keil C: abstarct pointer | 01/01/70 00:00 | |
| RE: keil C: abstarct pointer | 01/01/70 00:00 | |
RE: keil C: abstarct pointer | 01/01/70 00:00 |



