1. Problems with Changing Parameters in a function:
a. Recall: Function arguments are passed by value
i. When we write a function to call with a parameter, the parameter value is
copied into the calling parameter. Thus, the function technically has no
access to the address of the original value that was used as a calling
parameter, and thus cannot change it.
b. Returning Multiple Values
i. We cannot return two values with only one function.
c. Passing arrays to functions wastes memory and time.
2. Pointers pass their addresses of the values.
a. Memory is organized as sequence of bytes, and variables
allocated a prescribed number of bytes based on value type.
b. Local variables are allocated consecutive/adjacent
addresses in memory.
c. Each variable has an address in memory, which can be used
to access it.
b. Address and Dereference:
i. Address operator: &a is the address of the variable “a”
ii. Deference (indirection) operator: if “a” is an address location, then *a is
the variable at address a, or pointed to by “a”
1. Ex: *(1000) is ‘a’.
c. If a pointer “p” points to a variable “a”, then *p is a and p is &a.
i. To get address, P = &a
ii. To follow address, a = *P
d. Declaration and Assignment:
i. For every value type, there is a type POINTING to each value type.
1. “type” *p;
a. ex: int *p; b. double *pdbl;
c. char *cp;
d. Boolean *bp;
1. p = &a. Non-pointer types (int for example) can be assigned by us:
(for example, int x = 5)
2. For pointers, we don’t know address when it is initialized, hence
we ask program to initialize via “&”.
a. int *p;
p = &x;
b. int *p = &x;
3. The difference between *p and p is that if you were to assign a
variable to *p (var = *p), it would become the value
ASSIGNED into the address that “p” points to. If you were to
assign a variable to p (var = p), the variable would become the
ACTUALaddress location of the value it is pointing to (e.g. the
value location of x is 1000, so *p = x, and p = 1000)
i. int a = 3
int *p, *q
p = &a
q = p;
1. “p” points to the address of variable “a”, and “q” points the address
of “p”, which is the address of “a”.
ii. int a = 30
p = &a
y = *p
*p = 42;
1. “p” points the address of a, and y becomes the value of the address
that “p” points to. “*p = 42” changes the value of the address at
variable “a” to 42. iii. Points are not automatically initialized, meaning the address that it points
to is not initialized. Thus. *p = x can crash the program.
iv. Pointers of different value types cannot be mixed.
i. int i = 3, j = 5, *p = &I, *q = &j, *r;
1. p is equal to &*p
a. p == &*p == p == &(*p) = 1
2. **&p is the same as *(*(&p))
3. r = &x is an error, because they are different value types
4. (7*(*p))/(*q) + 7 = 11 and (7*(*p))/(*q)+7 = 11
a. Pointers are unary operators, thus they take precedence.
5. *(r = &j) *=* p = 15 and *(r = &j) *= (*p)
a. remember that “*” means both pointer and multiplication.
g. How they solve Shortcomings:
ii. Given “a” and “b” representing the numerator and denominator of a
fraction a/b, reduce the fraction to its lowest terms, namely cancel all
common factors of a and b. For example, 18/24 becomes ¾.
2. With this, we are no