UMBC CMSC 201 & 201H Fall '98 CSEE | 201 | 201 F'98 | lectures | news | help

An Example

The Program

/* File: bad_pointers.c This program uses pointers incorrectly. It won't compile. */ #include <stdio.h> main () { int a = 1, b = 2, c = 3 ; double x = 3.1, y = 3.2, z = 3.3 ; /* pointer declarations */ int *ptr1, *ptr2 ; double *dptr ; /* Can't assign integer expressions to pointers */ ptr1 = a + 7 ; ptr1 = 5 ; /* Exception: you can assign zero to a pointer */ ptr1 = 0 ; ptr1 = NULL ; /* Can't assign double address to integer pointer */ ptr1 = &x ; }

Output

% cc bad_pointers.c accom: Error: bad_pointers.c, line 20: illegal combination of pointer and integer, op = ptr1 = a + 7 ; ----------------^ accom: Error: bad_pointers.c, line 20: types : pointer to int versus int ptr1 = a + 7 ; ----------------^ accom: Error: bad_pointers.c, line 21: illegal combination of pointer and integer, op = ptr1 = 5 ; ------------^ accom: Error: bad_pointers.c, line 21: types : pointer to int versus int ptr1 = 5 ; ------------^ accom: Error: bad_pointers.c, line 28: Left pointer in assignment must have all qualifiers of right and pointed to types must be compatible (or void) (ANSI 3.3.16.1) ptr1 = &x ; -------------^ accom: Error: bad_pointers.c, line 28: Illegal pointer combination: pointer to int versus pointer to double ptr1 = &x ; -------------^ %


CSEE | 201 | 201 F'98 | lectures | news | help

Wednesday, 07-Oct-1998 21:39:38 EDT