Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 9: Designing Your Own Data Structures > Passing a struct As a Parameter

Passing a struct As a Parameter

Think back to the DVD-tracking program we've been discussing throughout this chapter. We started off with three separate arrays, each of which tracked a separate element. One array stored the rating field, another stored the movie's title, and the third stored a pithy comment.

We then introduced the concept of a structure that would group all the elements of one DVD together, in a single struct. One advantage of a struct is that you can pass all the information about a DVD using a single pointer. Imagine a routine called PrintDVD(), designed to print the three elements that describe a single DVD. Using the original array-based model, we'd have to pass three parameters to PrintDVD():

void    PrintDVD( char rating, char *title, char *comment ) {
    printf( "rating: %d\n", rating );
    printf( "title: %s\n", title );
    printf( "comment: %s\n", comment );
}

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint