Pointer Casting
- Updated2023-02-21
- 2 minute(s) read
A cast expression consists of a left parenthesis, a type name, a right parenthesis, and an operand expression. The cast causes the compiler to convert the operand value to the type that appears within the parentheses.
C programmers occasionally need to cast a pointer to one data type to a pointer to another data type. Because LabWindows/CVI does not restructure the user protection information for each cast expression, certain types of cast expressions implicitly disable run-time checking for the pointer value. In particular, casting a pointer expression to the following types disables run-time checking on the resulting value:
| Pointer to a pointer: | (AnyType **) PointerExpression |
| Pointer to a structure: | (struct AnyStruct *) PointerExpression |
| Pointer to an array: | (AnyType (*)[]) PointerExpression |
| Any non-pointer type: | (unsigned) PointerExpression, (int) PointerExpression, and so on |
Casting a pointer to one arithmetic type to a pointer to a different arithmetic type, such as (int *), (unsigned *), (short *), and so on, does not affect run-time checking on the resulting pointer nor does casting a pointer to a void pointer (void *).
Casting a Pointer to Dynamic Memory
Casts that you apply implicitly or explicitly to the void * values you obtain from malloc or calloc do not disable user protection.
When you allocate more memory than you need for one instance of the type you are casting to, LabWindows/CVI fills the available memory with as many instances as fit. Run-time checking signals an error when you try to access any remaining memory:
static int *values;
values = (int *)malloc (10);
values[0] = 1;
values[1] = 2;
values[2] = 3; /* error */
((char *)values)[8] = 'a'; /* error */
((char *)values)[9] = 'b'; /* error */