Chapter8 - Chapter 8 Pointers Pointers are largely an...

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

View Full Document Right Arrow Icon
Chapter 8 – Pointers Pointers are largely an entirely new concept, though there are connections with call-by- reference (references are a special kind of pointer). Pointer variables “point to” things (contain the memory addresses of things). They are declared by using ‘*’. int a; // declares a regular integer variable int *b; // declares a pointer variable int *c, d; // note – the * applies only to “c” // “a” and “d” are integer variables. “b” and “c” point to integer variables. Recall call-by-reference: double funny (int &x) { ……. . } Box “x” doesn’t contain an integer value. Instead it points to (refers to) an integer variable somewhere else. “x” is actually a somewhat special kind of pointer called a “reference”. The basic idea is the same but references are special in that, when we operate on them, we automatically get at the variable pointed to (referred to). Assignment to “x” places a value in the variable pointed to (referred to) by “x”. Pointers are a more manual proposition. The “&” operator (the same symbol as is used in call-by-reference, but with a different meaning) generates a pointer to something. This pointer value may then be placed in a pointer variable. int g, *t; double *s, h; t = &g; // make “t” point to “g” s = &h; // make “s” point to “h” Types are important. A pointer variable declared as a pointer to “int” cannot be made to point to a “double” variable (and vice versa). t = &h; // illegal – cannot assign “pointer to double” to “pointer to int” The something pointed to by a pointer may be accessed by using the ‘*’ (de-reference) operator. This is the same symbol that is used for multiplication, but it has a different meaning when applied to a pointer. In the case of pointers, “*b” means “the object that b points to”.
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
int a; *b; // declares a pointer variable a = 6; b = &a; // make “b” point to “a” *b = 4; // assign 4 to the variable pointed to by “b” cout << a; // will output 4 Pointer variables may be assigned to each other (provided that they point to the same kind of thing) and may also be compared. Two pointers are identical if they point to the same thing or both contain NULL. NULL is a universal pointer constant that can be assigned to pointer variables of any type. It can be thought of as a “pointer to nothing”. Attempting to de-reference a pointer containing NULL is a serious and all too common programming error. Exact effects vary from environment to environment but are not good! int a, *b, *c; b = &a; c = b; // “b” and “c” now both point to “a” *b = 6; *c = 12; cout << *b; // will output 12 *b = NULL; cout << *b; // NO NO NO! The “&” operator will not be used a lot in the course.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 8

Chapter8 - Chapter 8 Pointers Pointers are largely an...

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

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