Week04--newLecture2 - Memory Management Principles of...

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

View Full Document Right Arrow Icon
Memory’Management Principles’of’Programming Computer’Engineering’2SH4 Dr.’T.’E.’Doyle
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
2 Pointers A’pointer’is’a’variable’that’contains’the’ memory’address’of’a’variable. Pointers’are’used’to’express’some’ computaCon’and’to’write’more’compact’and’ efficient’code. ±areless’use’of’pointers’create’code’that’can’ be’impossible’to’read’and’understand.’
Background image of page 2
3 Pointers’and’Addresses Recall’the’organizaCon’of’computer’ memory’by’looking’at’a’simpliFed’ picture’to’the’right. Typically’memory’can’be’considered’an’ array’of’consecuCvely’numbered’ (addressed)’memory’cells. The’cells’may’be’manipulated’ individually’or’in’conCguous’groups. p: c:
Background image of page 3

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

View Full Document Right Arrow Icon
4 Pointers’and’Addresses’(cont±d) So’if’ c ’is’a’ char ’and’ p ’is’a’pointer’ of’type’ char ,’then’the’pointer’ p is’poinCng’to’the’char’ c . The’memory’cell’ p ’contains’the’ address’of’the’variable’ c . We’declare’a’pointer’ p , char *p; p: c:
Background image of page 4
5 Pointers’and’Addresses’(cont±d) Recall’the’unary’operator’ & ,’that’gives’the’address’of’ an’object: & ’only’applies’to’objects’in’memory,’such’as’variables’and’ array’elements. & ’cannot’be’applied’to’expressions,’constants,’or’register’ variables. So’the’statement: p = &c; assigns’the’address’of’ c ’to’the’variable’ p .’ p ’is’said’to’ “point’to”’ c .
Background image of page 5

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

View Full Document Right Arrow Icon
6 Pointers’and’Addresses’(cont±d) The’unary’operator’ * ’is’the’ indirec’on ’or’ dereferencing ’operator. When’applied’to’a’pointer,’it’accesses’the’ value’the’pointer’points’to. See’next’example…
Background image of page 6
7 Pointers’and’Addresses’(cont±d) Example: int x = 1, y = 2, z[10]; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ y = *ip; /* y is now 1 */ *ip = 0; /* x is now 0 */ ip = &z[0]; /* ip now points to z[0] */
Background image of page 7

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

View Full Document Right Arrow Icon
8 Pointers and Func1on Arguments Since’C’passes’arguments’to’func±ons’by’ value ,’there’is’no’direct’way’to’alter’a’variable’ in’the’calling’func±on.
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 26

Week04--newLecture2 - Memory Management Principles of...

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

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