Pointers

Pointers
©2005 Geoffrey Brown
David S. Wise
Chris Haynes
Bryce Himebaugh
Pointers !
A pointer, a.k.a. a reference, is the address of a location in
memory.
They are heavily used in almost all language implementations.
In most modern languages they cannot be accessed directly by
the programmer.
In C they can be manipulated directly and this is done
extensively.
They are very powerful, and dangerous (difficult to think about
and difficult to debug).
2
Pointer Picture
Memory
Variable Address Contents
...
int i = 3;
int *p = &i;
int j = *p;
i
100
3
...
p
500
100
...
j
600
3
...
Address values are arbitrary, but p
contains i’s address.
3
Reference Operators
Reference creation operator: &V, where V is a
variable or array reference, is a pointer to the
referenced part of memory.
Dereferencing operator: *E, where E is an
expression whose value is a pointer, is the value in
the location pointed to.
Or if on the LHS of an assignment, indicates
assignment to the referenced location.
Reference type operator: T *, where T is a type, is
the type of a pointer to something of type T.
4
Pointer Types
All pointer types are integer types, allowing all sorts of integer
operations to be performed on pointers.
Do not assume what size of integer a pointer is.
0 is the null pointer constant value.
void * is a pointer type that does not indicated the type of value
pointed to.
You can cast freely between any pointer and integer types, but
you had better know what you are doing!
It is easy to use an invalid pointer that points to garbage, in which
case the result is undefined (anything can happen)!
5
Array Pointers
In an expression, an array identifier a is the same as
&a[0].
a[i] is the same as *((a) + (i)).
or *(&a[0] + (i)).
6
constant array pointers
An expression that names a variable that contains an
array (rather than a pointer to an array) yields a constant
pointer (you cannot assign another pointer to it).
char *p;
char s[100];
p = s; /* ok */
s = p; /* syntax error, s is a constant pointer */
7
Indexing Style Examples
double a[] = {1, 2, 3};
double sum = 0;
// generally best
for (int i = 0; i < 3; i++) sum += a[i];
// equivalent, harder to read
for (int i = 0; i < 3; i++) sum += *(a + i);
// slightly more efficient, even harder to read
double *stop = a + 3;
for (double *p = a; p < stop; p++) sum += *p;
8
Pointer Addition
The result of p + i, where p has type T *, is p + i*sizeof(T), where + indicates simple addition.
Same for subtraction.
This is necessary for array subscripting to work, and
even handles multi-dimensional arrays.
This simple addition can be programmed in a way
exactly equivalent to p + i as
(T *) ((long)p + i * sizeof(T))
9
another example
char c = ’c’;
char s[] = "test";
char *p; /* p is a pointer to a char */
p = &c;
/* p points to variable c */
/* print the char p points to */
printf("%c", *p); // c
p = s; /* a string is a pointer to a
sequence of chars, ending in \0 */
printf("%s", p); // test
for (p = s; *p != ’\0’; p++) {
printf("%c\n", *p);
}
Draw picture on board
10
pointers and assignment
Pointers can be assigned and used to control where
assignment happens, as in this example:
int *ptr;
int k;
ptr = &k;
/* assign ptr the address of k */
k = 3;
/* assign k a value */
if (*ptr == k) {...} /* true ! */
*ptr = 4;
/* assign pointer dest a value */
if (k == 4) {...} /* true, dest was k */
Exercise: draw a picture of this
11
strings as char pointers
and as arrays
/* copy source string to dest */
char *array_strcpy(char dest[], char source[] {
int i = 0;
// should test dest, source for null
while (source[i] != ’\0’) {
dest[i] = source[i];
i++;
}
dest[i] = ’\0’;
return dest;
}
char *pointer_strcpy(char *dest, char *source) {
char *p = dest;
// should test dest, source for null
while (*source != ’\0’) {
*(p++) = *(source++);
}
*p = ’\0’;
return dest;
}
12
The Main FUnctioN
int main(int argc, char *argv[]) {
int i;
for (i = 0; i < argc; i++) {
printf("%s\n", argv[i]);
}
return 0;
}
$ gcc main.c
$ ./a.out a b c d
char **argv is the same as ./a.out
char *argv[], but not the same as a
char argv[][], which declares a
b
c
two dimensional array
d
13