Pointer Basics
A pointer is a variable that contains a
memory address
Pointers
pointer variables (like memory
CSE 130: Introduction to Programming in C
Spring 2005
addresses) are integers
Pointers are used to access memory and
manipulate addresses
Ex. scanf() takes a pointer to a variable
1
Pointer Addressing
Pointer Dereferencing
& is the (unary) address operator
If v is a variable, then &v is the memory
The dereference (indirection) operator * is
Ex:
NOTE: this value is NOT the value stored
used to access the value stored in a
pointed-to memory location
address at which v is stored
in the pointer!
int i, *p;
p = &i;
The pointer holds a memory address
* returns the value stored at that address
Pointer Examples
More Examples
int a = 7;
double x, y, *p;
int *p = &a; /* p points to a */
p = &x;
printf(*p = %d\n, *p); /* prints 7 */
y = *p; /* equivalent to y = *&x or y = x */
*p = 3; /* a is now 3 */
printf(a = %d\n, a); /* prints 3 */
int a, *p = &a; /* &a is ps initial value */
p = 0; /* only legal integer assignment */
int *p = &a, a; /* a must be declared first! */
Printing Pointers
Pointers to void
Pointers may not be assigned to one another
The %u format prints an address as an
unless they are of the same type (i.e., both
int*), or one of them is a pointer to void
unsigned decimal integer
The %p format prints an address in a
Pointer to void is a generic pointer type
system-specified format (ex. hexadecimal)
int *p; void *v;
int i = 77, *p = &i;
p = v; /* legal */
printf(is address: %u or %p, p, p);
v = p; /* legal */
prints is address: 234880252 or dfffcfc
v = 1; /* illegal */
7
Call-by-Reference
An Example
void swap (int *p, int *q)
Normally, variables are passed by value
their values are copied to the function
parameters
int tmp;
Variables not changed in the calling ftn
tmp = *p;
Using pointers, we can call by reference
Variables ARE changed in the calling ftn
*p = *q;
*q = tmp;
}
9
Pointers and Arrays
Pointer Arithmetic
Pointers and arrays are closely related
An array expression is a pointer to the first
If pa is a pointer to an array A:
A[i] can be written as *(pa + i)
Where pointers are concerned, (pa + 1)
element of the array
Thus, given an array A, pa = *A[0] means
that pa and A have the same value
10
points to the next object, regardless of the
variable type or size
This can also be written as pa = A;
BUT: A = pa is not legal (A is not a variable)
11
12
The Structure Pointer
Operator ( -> )
An Example
int A[] = {1, 3, 5, 7, 9};
int *pa = A; /* pa points to A[0] */
printf(%d, *pa); /* prints 1 */
printf(%d, *(pa + 3)); /* prints 7 */
pa++; /* pa now points to A[1] */
printf(%d, *pa); /* prints 3 */
Minus sign followed by greater-than sign
Usage:
pointer_to_structure -> member_name
Equivalent to:
(*pointer_to_structure).member_name
The parentheses are necessary here!
. and -> have equal precedence
13
Structure Pointers
struct student temp, *p = &temp;
temp.grade = A;
p -> lastName = Bushker;
p -> studentID = 590017;
printf(%c\n, (*p).grade);
15
14