{[ 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 "safer" string copy * Product types * C++ structs * Enumerations ++++++++++++++++++++++++++++++++++++++++++++++++++ One more thought about strcpy---there are two REQ clauses: * src/dest are C strings * 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); // REQ src is NUL-terminated string // dest has room for length+1 characters // MOD dest // EFF copy src to dest up to and including the first length characters; terminate dest with NUL { *src++ = *dest++; } *dest = '\0'; } This says that, as long as length is greater than zero *and* *src is non-NUL, copy the the caracter under src to dest, and advance both pointers. If length becomes 0, we've reached the limit, and we have to stop copying. If *src is NUL, we've copied all the "real" characters. In any event, we have to conclude dest with a 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
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
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. An example struct to hold grades might be: struct Grades { char uniq[9]; // uniqnames are 8 characters long + 1 NULL int midterm; int final; }; It is worth contrasting structs and arrays at this point. The elements in an array are homogeneous - they are all of the same type. The elements in a struct are heterogeneous - they can be (but don't have to be) different types. The elements in a struct are "directly accessible"---by name. The above statement declares the type "struct grades", but does not declare any objects of that type. We can define single objects of this type as follows: Grades bnoble; We can also initialize them using the same notation as arrays: Grades bnoble = {"bnoble", 60, 85}; Once we have a struct, we can access it's individual components using the "dot" operator: bnoble.midterm = 65; This changes the "midterm" element of bnoble to 65. Structs can be assigned to one another.
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