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

Info iconThis 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, [email protected] Administrivia • Project 2 due soon • Exam #1 – Thursday, October 6 in Lecture • Read Reek, Chapter 6: Pointers 2
Background image of page 1

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

View Full DocumentRight 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
Background 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
Background image of page 3

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

View Full DocumentRight Arrow Icon
The const modifier Indicates that a variable can't be changed, and enforced by
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/13/2012 for the course CMSC 216 taught by Professor Plane during the Spring '11 term at Maryland.

Page1 / 9

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

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

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