08+-+Structs+and+Enums - Pointer Arithmetic Recall Ve 280...

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

View Full Document Right Arrow Icon
5/12/2009 1 Structs and Enums Ve 280 Programming and Introductory Data Structures Pointer Arithmetic Recall y Remember that both of the following expressions are examples of pointer arithmetic: array[3] *(array+3) y Recall that an address is a location in memory. y Each byte in memory has a distinct address – that is, C++ is byte- addressable. y Every type has a “size” associated with it; the number of bytes of memory required to hold an object of that type. y The language standard does not guarantee much in the way of sizes. y For example, an “int” could be four bytes with one version of the compiler, and eight bytes on another version. Memory Addressability Type sizes y The amount of memory assigned to a data type is a source of innumerable “portability bugs” in programs. y For example, suppose someone writes a program that assumes that all “ints” are 8 bytes long. If that program is compiled on a 4- byte-int compiler, it is likely to break, if it compiles at all. y There are some guarantees, however: y A “char” is always one byte. y A “short” is always at least as big as a char. y An int is always at least as big as a short. y A long is always at least as big as an int. y However, while a “char” is always one byte, the restrictions on byte are strange: it must have at least eight bits, but could have more! Memory Addressability Type sizes y Since the sizes of types are not fixed, C++ provides an operator that you can use to determine the size of something, in bytes. For example: sizeof(int) tells you the number of bytes required to store an int. y This is very useful for pointer arithmetic. Suppose we have two arrays, one of chars and one of ints. (assume ints are four bytes long.) int ai[2]; char ac[2]; Memory Addressability Type sizes y Now, recall that if a is an array, and i is an int, the following two expressions are equivalent: a[i] *(a + i) y This seems strange, because the elements in ac and ai are different sizes! The i h ld b l int ai[2]; char ac[2]; 100: ai[0] 104: ai[1] intuition is a+i should be a pointer value that points to the object a[i] . y The sizeof() operator makes this happen. Since all pointers point to some specific type, and the compiler knows how big each type is, it can add the appropriate value to the pointer to get the right answer. 108: ac[0] 109: ac[1] 110: ????? Using Arrays Product Types y Write a program that keeps track of student grades. y For each student we need to know: 1. The student’s name 2. The midterm grade 3. The final exam grade y We could have three arrays: string names[233]; int midterm[233]; int final[233]; y However, it isn’t clear the arrays are related or that midterm[5] is the midterm grade belonging to the student named by names[5].
Image of page 1

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

View Full Document Right Arrow Icon
5/12/2009 2 On to Structs Product Types y Instead of three separate arrays, what we really want is a type that can “bind together” several other types into one “meta-type”.
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 ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern