Part D - Data Structures

Structure Walkthroughs

Outline a walkthrough technique for programs that include structure types

Technique | Exercises

Walking through a program that includes objects of structure type requires a slight modification to the original walkthrough table.  The table for programs without objects was initially described in the chapters on Walkthroughs and Functions

This chapter presents the augmented technique for walking through programs that include objects of structure type.

Augmented Technique

The following program contains several objects of type A.  The walkthrough table is shown below.

 // Structure Types - Walkthrough
 // struct_walk.c

 #include <stdio.h>

 struct A
         int x;
         double r;

 void foo(struct A* c);
 struct A goo(struct A d);

 int main(void)
         struct A a = {4, 6.67}, b;

         printf("00%d.%.3lf.111\n", a.x, a.r); 
         b = goo(a);
         printf("00%d.%.3lf.112\n", a.x, a.r);
         printf("%d.%.3lf.113\n", b.x, b.r);

 void foo(struct A* c)
         int i;

         i = c->x;
         c->x = c->r;
         c->r = c->x % i + 202.134;

 struct A goo(struct A d)
         struct A e;

         d.x = d.r - 62;
         e = d;
         return e;

The table includes:

  • the return type for each function
  • the name of each function
  • the structure type of each object
  • the name of each object
  • the type of each member
  • the name of each member

Note the breakdown of each object into its members in the head of the table.  We reserve a separate line for the addresses that are pointed to:

int void struct A
main() foo() goo()
struct A struct A struct A*   struct A struct A
a b c   d e
1000 100C 1018 101C 1020 102C
int double int double   int int double int double
x r x r   i x r x r



Previous Reading  Previous: Structure Types Next: Text Files   Next Reading

  Designed by Chris Szalwinski   Copying From This Site   

Creative Commons License