lect08 - CMSC 216 Introduction to Computer Systems Lecture...

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

View Full Document Right Arrow Icon
CMSC 216 Introduction to Computer Systems Lecture 8 Pointers Jan Plane & Pete Keleher {jplane, pete}@cs.umd.edu Administrivia • Project 2 due soon Exam #1 – Thursday, October 6 in Lecture Read Reek, Chapter 6: Pointers 2
Image of page 1

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

View Full Document Right Arrow Icon
P OINTERS Chapter 6, Reek 3 The -> operator Dereferencing of a pointer to a structure must occur before accessing a field of the structure; due to precedence, parentheses are needed Section s = {101, 25, 1300}; Section *sp = &s; *sp.num_students += 5; /* WRONG */ (*sp).num_students += 5; /* RIGHT */ C has a special operator to make this easier: – " (*sp).num_students " is equivalent to " sp->num_students " 4
Image of page 2
Generic pointers Pointers to void ( variables declared as void * ) can point to any type No casts needed with void * pointers You can't dereference a void * - you first need to cast or assign it to a real pointer type – the value obtained from a dereference depends on the type of pointer Must be careful to cast correctly 5 Type conversion with pointers Converting from one type to a pointer has some uses: unsigned int i; unsigned char *ch; i = 0x543210ab; ch = (unsigned char *) &i; printf(“%d\n”, *ch); printf(“%d\n”, * (unsigned char *) &i); Prints out either MSB or LSB of i , depending on architecture 6
Image of page 3

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

View Full Document Right Arrow Icon
The const modifier
Image of page 4
Image of page 5
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