{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}


08-structs-enums.student - Last time the array/pointer...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Last time: * the array/pointer duality * traversing arrays with pointers * String copy w/pointers Today: * A *very* compact string copy * Product types * C++ structs * Enumerations ++++++++++++++++++++++++++++++++++++++++++++++++++ Last time, we talked about strcpy. Here is a *very* compact way of writing it. void strcpy(char *d, const char *s) { while ((*dest++ = *src++)) { /* MT */ } } Let's talk about what that means. Here are some facts that help us understand it: * The assignment is evaluated every time the "boolean expression" is evaluated. * The language defines the "value of an assignment statement" to be a reference to the left hand side, post-assignment. So, executing this code: int foo = 1; int bar = 2; cout << (foo = bar) << endl; Prints "2" to standard out---the "value" of (foo = bar) is the value of foo, after the assignment happens. * ++ binds tighter than *, so we are incrementing the pointer values, *not* the characters to which they point. * The increments "happen after" the pointers are dereferenced and values/lvalues are yielded, not before. ++++++++++++++++++++++++++++++++++++++++++++++++++ One more thought about strcpy---there are two REQ clauses: * src/dest are C strings
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
* dest is "big enough" to hold the string in src. The first one isn't very commonly violated in real programs (and I would probably have left it out of my own code, because it's understood.) However, the second one is violated pretty commonly, so we might want to get rid of that REQUIRES clause. One way to do that is to change the function's interface: void strncpy(char *d, const char *s, unsigned int size); // EFFECTS: copy up to size-1 characters of s to d, and terminate // w/NUL ++++++++++++++++++++++++++++++++++++++++++++++++++ Product types. Suppose I wanted to write a program that kept track of students grades. For each student I need to know: The student's name The midterm grade The final exam grade I could have three arrays: string names[200]; int midterm[200]; int final[200]; But, it is not immediately clear that these arrays are related to one another, or that midterm[5] is the midterm grade belonging to the student named by names[5]. What we really want is a type that can "bind together" several other types into one "meta-type". This is called a "product" type: A product type describes a "compound object" that comprises one or more elements, each of independent type. C++ supports a product type: a "struct", short for structure.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 8

08-structs-enums.student - Last time the array/pointer...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon bookmark
Ask a homework question - tutors are online