Lab 7 Source Code

pointers.c

Either cut-and-paste the following into your lab, or

pointers.c

#include <stdio.h>

int main(void) {
    int x, y;
    int *p_int; /* Pointer to an int */

    x = 4;
    y = 0;
    p_int = &x; /* Pointer has address of x ("points to x") */

    printf("x=%d, y=%d, *p_int=%d, p_int=%X\n", x, y, *p_int, p_int);

    y = *p_int; /* Y now contains 4 also */
    p_int = &y; /* Pointer now has address of y ("points to y") */

    printf("x=%d, y=%d, *p_int=%d, p_int=%X\n", x, y, *p_int, p_int);

    *p_int = *p_int * 2; /* Multiply what p points to by 2 */

    printf("x=%d, y=%d, *p_int=%d, p_int=%X\n", x, y, *p_int, p_int);

    return(0);
}

functions.c

Either cut-and-paste the following into your lab, or

functions.c

#include <stdio.h> int f1(int a1) { int local1 = a1+1; return (local1); } int f2(int a1, int a2) { int local1; local1 = f1(a1); local1 += a2; return (local1); } int f3(int a1, int * p1) { int local1; local1 = f1(a1); *p1 = f1(a1); return (local1); } int main(void) { int x, y, z; x = 4; y = 7; z = 10; x = f1(x); y = f2(x, z); z = f3(x, &y); return(0); }