pointers.c - #include <stdio.h>#define SIZE 10 void fun_with_pointers_and_arrays void fun_with_strings void the_most_fun defined below int main int x

pointers.c - #include <stdio.h>#define SIZE 10 void...

This preview shows page 1 - 2 out of 4 pages.

#include <stdio.h> #define SIZE 10 void fun_with_pointers_and_arrays(); // defined below. void fun_with_strings(); void the_most_fun(); int main() { int x; // This is a declaration of the variable p of type // "int *", which means it is a pointer to an int. // A pointer is simply an address. int *p; x = 6; printf("x is initially %d\n", x); // The & operator is the "address of" operator, so that &x // returns the address x. In this case, the address of // x is being written into p, so that p points to x. p = &x; // p now points to x // The * operator is the dereferencing operator, which simply // indicates that a pointer should be followed. That is, // *p refers not to p itself, but to whatever p points // to -- in this case, p points to x. *p = 7; // This actually modifies x to be 7. printf("p points to %d\n", *p); printf("Now x = %d\n", x); fun_with_pointers_and_arrays(); // see the function definition below fun_with_strings(); // see below the_most_fun(); // see below } void fun_with_pointers_and_arrays() { printf("\nIn fun_with_pointers_and_arrays()\n"); // When you declare an array, such as the array "arr" below, // and specify its size, then space in memory is allocated // for the array. The name of the array, in this case arr, // is just a constant pointer to the first element of the array. int arr[SIZE]; // Notice that, in this case, arr and &arr[0] (the address of the // first element of a, are the same thing -- they're both addresses.
Image of page 1
Image of page 2

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture