CP1_9_Pointers_1112

CP1_9_Pointers_1112 - Pointers Pointers Memory address...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Pointers Pointers Memory address Memory address Every variable is stored somewhere in memory. As we saw in the array bounds demo, the memory address of a variable can be found using the address-of operator & . int i; double d; cout << &i << " " << &d; x22ccac x22cca0 0x22ccac 0x22cca0 address-of operator If a variable requires a sequence of consecutive memory locations to store its value, then the address-of operator gives the starting address of the sequence. 2 CSIS1117B Computer Programming 1 2011-2012 The addresses returned are of type pointer because we can think of them as pointing to the variable in memory We can declare variables of the pointer type to store the addresses of other variables. To do this, use * with the variable type we will point to. eclarin a pointer int j = 42; nt tr declaring a pointer variable, ptr, which will point to an int int* ptr; ptr = &j; setting it with the address of variable j. 3 CSIS1117B Computer Programming 1 2011-2012 j ptr now "points" to j int j = 42; int* ptr; ptr = &j; Memory address 42 j 004800 004804 tr &j 04800 … 77620 ptr = &j 004800 477620 4 CSIS1117B Computer Programming 1 2011-2012 Dereferencing operator Dereferencing operator Given an address, we use the dereferencing operator * to access the variable value at that address. emory address int i = 1; int j = 9; int* ptr; i 1 004800 04804 Memory address 88 001 ptr = &i; cout << *ptr << endl; * tr 88; j 9 004804 ptr 004800 477620 ... 1001 ptr 88; cout << i << endl; ptr = &j; tr 1001; Main memory 004804 *ptr = 1001; cout << j << endl; 1 88 1 88 Note that * has a different eaning in a eclaration 1001 1001 meaning in a declaration and in an expression. 5 CSIS1117B Computer Programming 1 2011-2012 Assigning values (from textbook) Assigning values (from textbook) 1 and p2 are pointers p1 and p2 are pointers assigns a pointer value ssigns a “pointed ” assigns a pointed-to variable value 6 CSIS1117B Computer Programming 1 2011-2012 Wild pointers Wild pointers A wild pointer is a pointer that points to garbage or to somewhere it shouldn’t. Wild pointers are very problematic. A ommon consequence is that the wild pointer allows some common consequence is that the wild pointer allows some...
View Full Document

Page1 / 18

CP1_9_Pointers_1112 - Pointers Pointers Memory address...

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

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