An Example
The Program
/*********************************************
** File: badpointers.c
** Author: R. Chang
** Modified by: Sue Evans
** Date: 3/2/04
** Section: 101
** EMail: bogar@cs.umbc.edu
**
** A sample program that shows how NOT to use pointers
** It doesn't compile, but many problems are reported
** just as warnings ... all of which will result in
** horrible errors when the program runs.
**********************************************/
#include
int main ()
{
int a = 1;
double x = 3.1;
/* pointer declarations */
int *ptr1;
double *dptr ;
/* Can't assign integer expressions to pointers */
ptr1 = a + 7 ;
ptr1 = 5 ;
/* Can't assign double expressions to pointers */
dptr = 6.78;
/* Exception: you CAN assign zero to a pointer */
ptr1 = 0 ;
ptr1 = NULL ;
/* Can't assign double address to integer pointer */
ptr1 = &x ;
return 0;
}
Output
linux1[80] % gcc -Wall -ansi badpointers.c
badpointers.c: In function `main':
badpointers.c:27: warning: assignment makes pointer from integer without a cast
badpointers.c:28: warning: assignment makes pointer from integer without a cast
badpointers.c:31: incompatible types in assignment
badpointers.c:38: warning: assignment from incompatible pointer type
linux1[81] %