8 Pointers and Make

8 Pointers and Make - 2/22/11 CMSC 216 Introduction to...

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

View Full Document Right Arrow Icon
2/22/11 1 CMSC 216 Introduction to Computer Systems Lecture 8 I/O, cont. & Pointers Jan Plane & Pete Keleher {jplane, [email protected] Administrivia • Project 1 – grades will be available on grades server soon • Project 2 posted • Exam #1 – Thursday, March 3 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
2/22/11 2 P OINTERS ( CONT .) 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
2/22/11 3 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
2/22/11 4 The const modifier Indicates that a variable can't be changed, and enforced by compiler int const i = 4; const int j = 5; i++; /* ERROR */ j++; /* ERROR */ Order of type specifier and const modifier does matter when dealing with pointers: int i = 4, j = 5; const int *p = &i; /* pointer to constant int */ int * const q = &j; /* constant pointer to int */ p = &j; /* OK */ *p += 5; /* ERROR */ q = &i; /* ERROR */ *q += 23; /* OK */ The program cdecl can be useful for decoding some more complex declarations 7 Incrementing pointers Pointers can be incremented/decremented just like integer type variables, "moving" one element at a time – how much is added to the address depends on the size of the type to which the pointer points (as declared)
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 11/16/2011 for the course CMSC 216 taught by Professor Plane during the Spring '11 term at Maryland.

Page1 / 13

8 Pointers and Make - 2/22/11 CMSC 216 Introduction to...

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