22pointers_3014

22pointers_3014 - Lecture File 22 COP 3014 January 17, 2008...

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

View Full Document Right Arrow Icon
Lecture File 22 COP 3014 January 17, 2008 A. Ford Tyson 1 COP 3014 Lecture File 22 ± Introduction to 1 – Pointers – References – Dynamic Memory Allocation and Dynamic Variables Copyright 1997-present, Ann Ford Tyson Pointers ± pointer – stores a memory address – a positive integer or 0 2 – 0 is special, a NULL address ("points" to nothing) ± range of values – depends upon the platform you use – 0 up through some maximum Pointers p.2 ± legal operations – assignment – comparison ( == , < , > etc. ) 3 – arithmetic: addition and subtraction only ± uses – can access a memory location via a pointer – can set up dynamic variables and data structures
Background image of page 1

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

View Full DocumentRight Arrow Icon
Lecture File 22 COP 3014 January 17, 2008 A. Ford Tyson 2 Pointers p.3 ± dynamic variable – a variable which can only be accessed via a pointer 4 – created "on the fly" during a program run – memory space for its storage is allocated during the run – is not declared, hence has no name Declaring Pointer Variables ± int* p; /* p, q are pointer variables */ int* q; /* data type is pointer to int */ /* must store the memory */ 5 /* address of an int */ /* "bound" to type int */ ± int num, val; /* num and val are */ /* type int */ What's in memory? name 1002 1001 ? ? q p address contents note: we are simplifying addressing for our discussion 6 num 1003 1004 1005 1006 1007 ? ? ? ? ? val
Background image of page 2
Lecture File 22 COP 3014 January 17, 2008 A. Ford Tyson 3 Now, execute these statements ± num = 5; p = &num; 7 ± & is the address operator What's in memory now? name 1002 1001 ? 1003 q p address contents 8 1003 1004 1005 1006 1007 5 ? ? ? ? num val another common representation p 9 5 num
Background image of page 3

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

View Full DocumentRight Arrow Icon
Lecture File 22 COP 3014 January 17, 2008 A. Ford Tyson 4 Next, consider ± val = *p; /* sets val to contents */ /* of the location p */ /* "points to" */ 10 /* effect: same as */ /* val = num */ /* access "indirectly" */ ± * is the dereferencing operator ± both * and & are unary and have high precedence, right-left associativity What's in memory then?
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.

Page1 / 12

22pointers_3014 - Lecture File 22 COP 3014 January 17, 2008...

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