CProjects - six short assignments to be implemented in the C programming language
CSSE 221 – Fundamentals of Software Development Honors
Fall 2008–2009
Work on these exercises
individually,
but be quick to get help as needed from your student assistants,
your instructor and your classmates.
CProject: Dog (structures)
Overview
This project introduces you to structures. Key ideas include:
- Use a typedef to define a structure type.
- Throughout,
use pointers to structure objects rather than structure objects themselves.
Following this Java style will keep you out of trouble.
- Don't forget to malloc space for pointers to point to,
much as new does in Java.
- Don't forget to free previously malloc'ed space
when it is no longer needed.
Unlike Java, you cannot rely on a garbage collecter.
Instructions
- Construct new CProject that is an ANSI C Project
called Dog.
- Add a new Header File to the src folder,
choosing the "Default C header template" (not the C++ one)
and calling the file Dog.h.
- #include "Dog.h" in Dog.c.
- In Dog.h, define a structure called Dog that has the following fields:
- A name that is a C-style string of at most 6 characters.
- A breed that is a C-style string of unlimited length.
- An owner that is a C-style string of unlimited length.
- An age that is an integer.
- A weight that is a float.
- In function main, declare a pointer to a Dog.
- Get your work so far graded,
by your instructor, student assistant or a newly-promoted student assistant.
- The grader should refer to the written answer key.
- Write and test the following pair of functions (put their prototypes in Dog.h):
- constructDog
- It takes an age, allocates space for a Dog, sets the allocated Dog's age to the given age,
and returns a pointer to the allocated Dog space.
- Ignore the other fields of Dog for now.
- printDog
- It takes a pointer to a Dog and prints the dog's age.
- Ignore the other fields of Dog for now.
- Extend your pair of functions to deal with the following
(write and test them one by one, in the order listed):
Assume (i.e. do NOT check) that the string arguments to constructDog
are valid C-style strings.
Also assume that the argument passed for the name is at most 6 characters.
However, you must allow the breed and owner arguments to be strings of any length.
- Get your work so far graded,
by your instructor, student assistant or a newly-promoted student assistant.
- The grader should refer to the written answer key.
- With the grader, try passing in strings of length 7, 8, 9 and 20 for the dog's name.
- For some of these, the run should execute as if nothing was wrong.
- For others of these, you should see spurious characters.
- Worse still, you should sometimes see
unrelated variables modified by the buffer overflow.
- Write a destructDog function
that takes a Dog pointer and deallocates space for that Dog,
including the malloc'ed space for its fields.
- Get your work so far graded,
by your instructor, student assistant or a newly-promoted student assistant.
- The grader should refer to the written answer key.
- Turn in your work by adding and committing your project to your individual repository.