|
|||||||||||||||||||||||||||
|
Technical Support On-Line Manuals C251 User's Guide |
C251 User's GuidePointer ConversionsThe C251 compiler can convert the memory types of pointers. Pointer conversions can be forced by explicit program code using type casts or can be coerced by the compiler. A generic pointer is identical to a near * pointer definition for the TINY or XTINY memory model. For all other memory models a generic pointer is the same as a far * pointer definition. The C251 compiler converts a memory specific pointer into a generic pointer when the memory specific pointer is passed as an argument to a function which requires a generic pointer. This is the case for C251 run time library functions such as printf, sprintf, and gets which use generic pointers as arguments. For example:
extern int printf (void *format, ...);
extern int myfunc (void near *p, int data *dq);
int data *dp;
const char near *fmt = "value = %d | %04XH\n";
void debug_print (void) {
printf (fmt, *dp); /* fmt is converted */
myfunc (fmt, nx); /* no conversions */
}
In the call to printf, the argument fmt which represents a 2 byte near pointer is automatically converted or coerced into generic pointer. This is done because the prototype for printf requires a generic pointer as the first argument. Depending on the memory model the size of a generic pointer is 2 bytes or 4 bytes. Note
Pointer conversion are mostly done by extending or truncating the high part of the pointer, since the address space is viewed as a linear entity. The following table details the process involved in converting pointers.
All pointer conversions not listed in this table are illegal and generate a WARNING message at compile time. The following example listing illustrates a few pointer conversions.
stmt level source
1 int *p1; /* generic ptr (3 bytes) */
2 int xdata *p2; /* xdata ptr (2 bytes) */
3 int near *p3; /* near ptr (1 byte) */
4 int code *p4; /* code ptr (2 bytes */
5
6 void pconvert (void) {
7 1 p1 = p2; /* xdata* to generic* */
8 1 p1 = p3; /* near* to generic* */
9 1 p1 = p4; /* code* to generic* */
10 1
11 1 p4 = p1; /* generic* to code* */
12 1 p3 = p1; /* generic* to near* */
13 1 p2 = p1; /* generic* to xdata* */
14 1
15 1 p2 = p3; /* near* to xdata* (WARNING) */
*** WARNING 259 IN LINE 15 OF P.C: pointer: different mspace
16 1 p3 = p4; /* code* to near* (WARNING) */
*** WARNING 259 IN LINE 16 OF P.C: pointer: different mspace
17 1 }
| ||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||