# CPSC 259 Chapter 7.5: Generic pointers and connecting pointers

30 views2 pages
6 Aug 2016
School
Course
Few distinctions to take note of in the following code:
Int a = 5;
Int* p;
P = &a
The value of a = 5, the value of p is the address of a, p = &a, and the value of *p = a
Changing the value of a, would not change the value of p, as the address is still the
same, but it will change the value of *p. The only way to change the value of p to
another variable, is either by assigning p = &variable, or by equating p to a different
pointer, so p = t, where t == &b. Also, note that we can always access a variable’s
address by using the &command, so we do not necessarily need a pointer to access a
Generic pointers:
You could specify the types of pointers, when you are first initializing them that is int * p
would point to integers and char * p would point to characters. You can also have
generic pointers, that can point to anything, no questions asked, but when deferring the
value stored in the pointee, you need to cast the pointer.
For initializing a generic pointer, you use:
Void * p; and For deferencing the value of the pointer, you use
a = *(int *)p . You would normally defer a pointer by *p, but since you want to defer a
generic pointer to an integer value, you explicitly cast the pointer p to a generic type
before pointing it to a particular value.
Pointing at Pointers:
You can also refer pointer to pointers. So, if you a pointer p that is referring to a, then
the p stores the address of a. To store the address of p, you refer it to another pointer, q
that stores the address of p, but before you can do that, you need to use ** . So
int * p = &r and int ** q = &p.
To set another pointer r to q, you use int *** r = &q.
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

# Get access

\$10 USD/m
Billed \$120 USD annually
Homework Help
Class Notes
Textbook Notes