CSC161 2011S Imperative Problem Solving

Laboratory: Structured Types in C

Summary: In this laboratory, you will explore different issues relating to structured types in C.

Contents:

Preparation

a. Create a new directory for this laboratory.

b. Create a Makefile in that directory.

c. Create a simple C file that you can use to add declarations, code, and other experiments.

Exercises

Exercise 1: Simple Structs

Here's a simple declaration of a type named struct dog, which is quite similar to the type given in the text.

struct dog { int x; int y; };

Write a program that declare a variable of type struct dog, assigns values to the two fields, and prints out those values.

Exercise 2: Printing Simple Structs

a. Write a function, dog_print (struct dog d), that prints out a variable of type struct dog.

b. Create a variable of type struct dog, fill in its two fields, and then use dog_print to print it out.

c. Create a variable of type struct dog, don't fill in its two fields, and then use dog_print to print it out.

Exercise 3: Mutating Structs

Consider the following function.

void
dog_mutate (struct dog d)
{
  printf ("Old: ");
  dog_print (d);
  ++d.x;
  --d.y;
  printf ("New: ");
  dog_print (d);
} // dog_mutate
 

Consider the following sequence of code that uses dog_mutate.

  struct dog bingo;
  bingo.x = 4;
  bingo.y = 2;
  printf ("Original: ");
  dog_print (bingo);
  dog_mutate (bingo);
  printf ("Updated:  ");
  dog_print (bingo);

a. What do you expect the output to be?

b. Check your answer experimentally.

Exercise 4: 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. What, if anything, do K&R say on the subject?

b. What did the previous exercise suggest about that.

Exercise 5: Struct Equality

Declare two variables of type struct dog and determine whether you can successfully assign one such variable to another.

Exercise 6: Struct Variables

Here are two ways to declare variables of type struct dog.

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

a. What do you expect to have happen if you assign dingo to wolf?

b. What do you expect to have happen if you assign wolf to 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 7: Naming Structs

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 8: 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. Check your answer experimentally.

c. What do you expect to have happen if you use an anteater as the actual parameter to a function that expects an aardvark as a parameter?

d. Check your answer experimentally.

e. What do you expect to have happen if you use a pointer to an anteater as the actual parameter to a function that expects a pointer to an aardvark as a parameter?

Exercise 9: Not-so-equivalent Structs

Here's yet another related declaration.

typedef struct eaterofants ( int y; int x; } eaterofants;

Consider the following code:

  aardvark a;
  eaterofants e;
  a.x = 3;
  a.y = 4;
  e = *((eaterofants *) &a);
  printf ("e = (%d,%d)\n", e.x, e.y);

a. Do you expect the C compiler to permit this?

b. Check your answer experimentally.

c. What output do you expect this to produce?

d. Check your answer experimentally.

Exercise 10: 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. Check your answers 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. Check your answers experimentally.

Exercise 11: The Size of Structures

a. Determine the size of struct lion.

b. Given a variable of type struct lion, determine (i) the location of that variable; (ii) the location of its first field; nad (iii) the location of its second field.

Exercise 12: The Size of Structures, Revisited

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. Check your answer experimentally.

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

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

e. Explain your results.

 

History

Wednesday, 12 February 2003 [Samuel A. Rebelsky]

  • Created.
  • Revamped (after class, in preparation for next year).
    • New introductory problems.
    • New Java vs. C problem.

Tuesday, 16 November 2010 [Samuel A. Rebelsky]

Tuesday, 19 April 2011 [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 Wed Apr 20 11:47:09 2011.
The source to the document was last modified on Wed Apr 20 11:47:03 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Labs/structs-lab.html.

Samuel A. Rebelsky, rebelsky@grinnell.edu