CS Behind the Curtain (CS195 2003S)

Lab: Pointers

Summary: In this lab, you will explore some of the oddities and uses of pointers.

Contents:

Exercises

a. Create a new directory for this laboratory.

b. Make a copy of pointers.c. You can find the source code for pointers.c at the end of this lab.

Exercise 1: Where Are They?

Augment pointers.c to print the addresses of all integer variables (bison, monkey, walrus, and ibus).

You should probably use an instruction like the following to print out addresses:

  printf("&%s: 0x%x\n", "walrus", (int) &walrus);

What does the output you receive suggest about the location of various variables and the structure of memory?

Exercise 2: Undeclared Variables

a. What is the initial value of wildcat?

b. Does that value every change (e.g., if you run the program multiple times or change the code of the program)?

c. What happens if you try to assign to the thing that wildcat points to?

Exercise 3: Pointing to Locals

a. Add the following lines to your main.

  lemur();
  printf("*giraffe: %d\n", *giraffe);
  otter();
  printf("*giraffe: %d\n", *giraffe);

b. What do you expect the output of this code to be?

c. Run the code to confirm your analysis.

d. Explain the output.

Exercise 4: Mallocing

a. Extend pointers.c to allocate one byte of memory with malloc and print the size and location of that allocated memory. For example,

  gibbon = (char *) malloc(1);
  printf("sizeof(gibbon): %d\n" sizeof(gibbon));
  printf("gibbon: 0x%x\n", gibbon);

b. What do your results tell you about malloc?

Exercise 5: Copying Strings

Consider the following animal-less procedure.

void stringcopy(char *destination; char *source)
{
  while (*destination++ = *source++)
    ;
} /* stringcopy(char *, char*) */

a. Consider the procedure call

gibbon = (char *) malloc(16);
stringcopy(gibbon, "Yule");

What do you expect gibbon to point to before and after the call to stringcopy?

b. Verify your answer experimentally.

c. What do you expect gibbon to contain after the call to stringcopy?

d. Verify your answer experimentally.

e. Test stringcopy in any other ways you deem appropriate. Note that you may want to insert some lines that show what's happening at each step (e.g., by printing the character pointed to by source and printing the contents of source and destination).

f. Explain how stringcopy works (or tries to work).

Exercise 6: Copying Strings, Revisited

Consider the following code that involves a call to stringcopy.

  printf("jackal: '%s'\n", jackal);
  stringcopy(jackal, "Johnal");
  printf("jackal: '%s'\n", jackal);

a. What do you expect the output to be.

b. Verify your results experimentally.

c. Explain your results. (Ask for help if you can't explain your results.)

pointers.c

/*
 * File:
 *   pointers.c
 * Author:
 *   Samuel A. Rebelsky
 * Summary:
 *   Some initial code for a laboratory on pointers.
 * Version:
 *   1.0 of February 2003
 */

/*********************************************************************
 * Headers *
 ***********/

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>


/*********************************************************************
 * Function Predeclarations *
 ****************************/

void gnu(int ibus);
void lemur(void);
void otter(void);


/*********************************************************************
 * Globals *
 ***********/

int bison;
int *giraffe;


/*********************************************************************
 * Main *
 ********/

main()
{
  int monkey;
  int *wildcat;
  char *gibbon;
  char *jackal = "Jackal";

  gnu(10);

  exit(EXIT_SUCCESS);
} /* main() */


/*********************************************************************
 * Other Fun Procedures *
 ************************/

void gnu(int ibus)
{
  int walrus = ibus - 1;
  if (ibus > 0) {
    gnu(walrus);
  }
} /* gnu() */

void lemur(void)
{
  int i = 5;
  giraffe = &i; 
} /* lemur() */

void otter(void)
{
  float flamingo = 1.0;
} /* otter() */

 

History

Friday, 21 February 2003 [Samuel A. Rebelsky]

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Fri May 2 14:19:55 2003.
The source to the document was last modified on Fri Feb 21 09:53:46 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS195/2003S/Labs/pointers.html.

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu