CS Behind the Curtain (CS195 2003S)

Laboratory: Structs and Other User-Defined TYpes

Summary: In this laboratory, you will explore different issues relating to pairs and structures.

Contents:

Exercises

Exercise 0: Preparation

a. Review the readings on structs, unions, and typedef.

b. Create a new directory for this laboratory.

Exercise 1: Simple Structs

Here's a simple declaration of a type named struct dog.

struct dog { int x; int y; };

Write a program that declares two variables of type struct pair and determines whether you can successfully assign one such variable to another.

Exercise 2: Naming Structs

Here's a simple attempt to name the struct from the previous exercise dingo.

struct dog { int x; int y; } dingo;

a. What do you expect to have happen if you assign a variable of type dingo to a variable of type struct dog?

b. What do you expect to have happen if you assign a variable of type struct dog to a variable of type dingo?

c. See what splint reports for each case.

d. See what happens when you try to compile a program that does that assignment.

Exercise 3: Naming Structs, Revisited

Here's a simple declaration for a aardvark structure.

typedef struct aardvark { int x; int y; } aardvark;

a. Is this a valid type definition?

b. What type name or names does it define? (I'll ask you to answer this question as a class after lab.)

Exercise 4: Equivalent Structs

Here's a related declaration for a anteater structure.

typedef struct anteater { int x; int y; } anteater;

a. Do you expect to be able to assign a variable of type anteater to a variable of type aardvark or vice versa?

b. Verify your results experimentally.

Exercise 5: Naming Structs, Re-Revisited

Here's another simple type declaration.

typedef struct lion { int x; int y; } tiger, bear;

a. What do you expect the effect of this declaration to be?

b. Verify your results experimentally.

c. Do you expect to be able to assign a variable of type tiger to a variable of type bear or vice versa?

d. Verify your results experimentally.

Exercise 6: The Size of Structures

Consider the following structures:

typedef struct wombat { int x; char y; } wombat;
typedef struct vixen { double x; int y; } vixen;

a. Assuming that integers take 4 bytes and characters take 1 byte, how many bytes should a wombat take?

b. Verify your answer experimentally.

c. Assuming that doubles take 8 bytes and ints take 4 bytes, how many bytes should a vixen take?

d. Verify your answer experimentally. You may want to verify my claims about the size of primitive types.

e. Explain your results.

Exercise 7: Pass-By-Value or Pass-By-Reference

As you may have noted, primitive values in C are passed by value. That is, if you change the value of a formal parameter, it does not affect the underlying actual parameter.

Arrays, on the other hand, appear to be passed by reference. That is, if you change an element in a formal parameter array, the corresponding element of the corresponding actual parameter array changes. (In actuality, the address of the array is passed by value, but we'll leave that discussion until later.)

a. Do you think structures are passed by reference or by value?

b. What, if anything, do K&R say on the subject?

c. Write a program that helps you verify what you learned in the previous steps.

Exercise 8: C Structs vs. Java Classes

Consider the following two similar programs, one in Java, one in C.

public class Cheetah 
{
  int val;
  public static void main(String[] args)
  {
    Cheetah chester = new Cheetah();
    Cheetah charlie = new Cheetah();
    chester.val = 5;
    charlie = chester;
    charlie.val = 10;
    System.out.println("Charlie: " + charlie.val);
    System.out.println("Chester: " + chester.val);
  } // main(String[])
} // class Cheetah
#include <stdio.h>
typedef struct { int val; } cheetah;
main()
{
  cheetah chester;
  cheetah charlie;
  chester.val = 5;
  charlie = chester;
  charlie.val = 10;
  printf("Charlie: %d\n", charlie.val);
  printf("Chester: %d\n", chester.val);
  exit(0);
} /* main() */

a. What do you expect the output of the Java program to be?

b. What do you expect the output of the C program to be?

c. What do the similarities or differences suggest about the similarities or differences between Java and C?

Exercise 9: The Size of Unions

a. What do you expect the size of zebra to be in the following type definition:

typedef union zebra { int x; int y; } zebra;

b. Verify your result experimentally.

c. Explain why one might define such a type.

d. What do you expect the size of yak to be in the following type definition?

typedef union yak { int i; char ch; } yak;

e. Verify your answer experimentally.

f. What do you expect the result of the following code to be?

   yak yolanda;
   yolanda.i = 10;
   yolanda.ch = 'a';
   printf("yolanda.i: %d\n", yolanda.i);

g. Verify your answer experimentally.

h. Explain your results.

Exercise 10: An Interesting Union

Consider the following type definition

typedef union lemur { 
  int i; 
  struct { char b1,b2,b3,b4; } bytes; 
} lemur;

a. What size is a lemur on our system?

b. Why might someone define such a type?

c. Write a small program that illustrates that use.

d. Why might someone criticise this type and its usage? (We'll discuss this issue in the next class session.)

Exercise 11: Uninitialized Structs

a. Assume that you've defined the point type as above. What do you expect the result of the following lines to be?

  point p;
  printf("(%d,%d)\n", p.x, p.y);

b. Verify your results experimentally.

c. Explain your results.

 

History

Wednesday, 12 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:59 2003.
The source to the document was last modified on Wed Feb 12 22:21:11 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS195/2003S/Labs/structs.html.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu