/*------------------------------------------------------------------*/ /* ptrs.c */ /* A Nonsensical, Non-Compiling Program to Illustrate Pointers. */ /*------------------------------------------------------------------*/ int main(void) { /*---------------------------------------------------------------*/ /* Pointer Fundamentals */ /*---------------------------------------------------------------*/ int i1; /* i1 is a variable of type int. */ int i2; /* i2 is a variable of type int. */ int *pi3; /* pi3 is a variable of type int*. */ /* pi3 is an integer pointer. */ int* pi4; /* pi4 is a variable of type int*. */ /* Spacing before and after "*" doesn't matter. */ i1 = 5; pi4 = 6; /* Compiletime warning: type mismatch. */ pi3 = i1; /* Compiletime warning: type mismatch. */ i1 = pi3: /* Compiletime warning: type mismatch. */ pi3 = &i1; /* "&" is the "address of" operator. */ pi3 = 6; /* Still compiletime warning. */ *pi3 = 6; /* "*" is the "dereference" operator. */ /* Changes value of *pi3 and i1. */ /* *pi3 and i1 are aliases. */ /* Here: undesirable. Elsewhere: useful. */ *pi4 = 7; /* Runtime error. Segmentation fault, or memory corruption. */ pi4 = &i2; /* Hereafter, *pi4 and i2 are aliases. */ i2 = *pi3; /* Assigns one int to another. */ *pi4 = *pi3; /* Same as previous. Assigns one int to another. */ pi4 = pi3; /* Assigns one memory address to another. */ /* *pi3 and *pi4 are now aliases. */ pi4 = &i2; /* Restore pi4 to previous value */ /* Note: & and * are inverse operators: &*pi3 is the same as pi3 *&i1 is the same as i1 */ /*---------------------------------------------------------------*/ /* The NULL pointer */ /*---------------------------------------------------------------*/ pi3 = NULL; /* Indicates that pi3 points to no valid memory location. */ /* NULL is a #defined constant in several standard header files. */ /* #define NULL (void*)0 */ *pi3 = 8; /* Runtime error: Segmentation fault. */ /* Note that NULL differs from "unpredictable value." */ pi3 = &i1; /* Restore value of pi3 */ /*---------------------------------------------------------------*/ /* Pointers and Relational Operators */ /*---------------------------------------------------------------*/ if (*pi3 == *pi4) ... /* Compares two ints. */ /* Evaluates to TRUE (1). */ if (pi3 == pi4) ... /* Compares two memory addresses. */ /* Evaluates to FALSE (0). */ if (pi3 != pi4) ... /* Evaluates to TRUE (1). */ /* Note: if (pi3 == pi4) is TRUE, then (*pi3 == *pi4) is certainly TRUE if (*pi3 == *pi4) is TRUE, then (pi3 == pi4) may or may not be TRUE if (pi3 == NULL) ... /* Evaluates to FALSE */ return 0; }