Escolar Documentos
Profissional Documentos
Cultura Documentos
Pointers in C
Pointer is a variable (data object) which has as a value an address of
another variable.
P
12333
12333 (address)
Variable of type
Object Type
c Teresa Leyk
° Slide 1 Pointers
CPSC 211 Data Structures & Implementations
c Teresa Leyk
° Slide 2 Pointers
CPSC 211 Data Structures & Implementations
Before After
Allocate memory ?? ??
P P
?? for an object
Before After
ID G ID G
?? ?? Set values 12 5.5
P P
Before After
ID G
12 5.5 Free object
P P
??
c Teresa Leyk
° Slide 3 Pointers
CPSC 211 Data Structures & Implementations
c Teresa Leyk
° Slide 4 Pointers
CPSC 211 Data Structures & Implementations
Examples
Consider the block of storage referenced by int pointers pa and pb. The
assignments: *pa = 5; and *pb = 10; are correct. But the
instruction pa = 17 is incorrect because there is mismatch of types
(type pointer to int and int are not the same). The instruction *pa =
*pb copies the value in the storage pointed by pb to the storage pointed
by pa. If we have pa = pb; then both the pointer variables reference to
the same storage (which contains 10 in this case). They are called aliases.
The block of storage such that no pointer references to it becomes
inaccessible. The function free(pa); returns the block of storage
referenced by pa to the pool of available storage. In C, there is no
automatic garbage collection. Recycling of storage must be managed by
the programmer. If we have the situation: pa = pb; and free(pb);
then pa is a dangling pointer, that is, a pointer which references to an
inaccessible block of storage. This is a source of difficult-to-find bugs.
c Teresa Leyk
° Slide 5 Pointers
CPSC 211 Data Structures & Implementations
c Teresa Leyk
° Slide 6 Pointers
CPSC 211 Data Structures & Implementations
c Teresa Leyk
° Slide 7 Pointers
CPSC 211 Data Structures & Implementations
c Teresa Leyk
° Slide 8 Pointers
CPSC 211 Data Structures & Implementations
int main()
{
int a = 2, b = 3;
printf("before: a = %d, b = %d\n", a, b);
swap(&a, &b); /* & is necessary here */
printf("after: a = %d, b = %d\n", a, b);
}
c Teresa Leyk
° Slide 9 Pointers
CPSC 211 Data Structures & Implementations
Memory Allocation
c Teresa Leyk
° Slide 10 Pointers
CPSC 211 Data Structures & Implementations
Examples
typedef struct {
int ID;
double GPA; /* grade */
} Student;
Student *sptr;
sptr = (Student *) malloc(sizeof(Student));
sptr->ID = 2310;
sptr->GPA = 3.4;
c Teresa Leyk
° Slide 11 Pointers
CPSC 211 Data Structures & Implementations
Memory Deallocation
The allocated memory remains in use until the program has finished or
memory is released using the function free.
Example of a memory leak:
void section() {
Student *p;
p = (Student *) malloc(35*sizeof(Student));
return; /* wrong! */
}
After executing the function section the pointer p is removed from the
stack but the allocated memory is not released and it cannot be accessed.
To access the array outside section a pointer to an array must be
returned.
Student *section() {
Student *p;
p = (Student *) malloc(35*sizeof(Student));
return p; }
c Teresa Leyk
° Slide 12 Pointers
CPSC 211 Data Structures & Implementations
Operations on Pointers
&i address of a referenced location
*p contents of a referenced location
p++ value of the pointer p incremented by one unit of base type size (p
= p + sizeof(base type))
++p increments the pointer p = p + sizeof(base type) then
gets the value of p
p = &i assigns a memory location to the pointer
type *p declares a pointer and its base type
c Teresa Leyk
° Slide 13 Pointers
CPSC 211 Data Structures & Implementations
Arrays
A pointer can be treated as just a memory address of a block of storage.
But this interpretation of a pointer is not always useful, and we will treat
pointers in a more abstract way. A very useful interpretation is to see a
pointer as a generalization of an index of a vector (array). Especially in C
you can easily notice this relation between pointers and vector indices.
double A[100]; /* elements start from 0,
that is, A[0],A[1],...,A[99] */
double *pa = &A[3]; /* pa points to the 4th
element of A */
double *pa1 = &A[5]; /* the same can be done as
follows: */
pa = A + 3; /* pointer arithmetic is involved here! */
/* here we get that the distance between
pa1 and pa is 2 = 5-3 */
printf("distance between pointers pa"
" and pa1 is = %d\n", pa1 - pa);
c Teresa Leyk
° Slide 14 Pointers
CPSC 211 Data Structures & Implementations
The name of an array is a pointer to the first element of the array. The
array name is a constant pointer, it cannot be changed by performing
pointer arithmetic. Note that C compiler does not check bounds of an
array.
There is no pointer arithmetics in Pascal and Java.
In languages where pointers are unavailable (as for instance Fortran77)
we can still use vector indices to emulate pointers but we lose their
dynamical feature (the possibility of changing the size of a data structure
during runtime).
c Teresa Leyk
° Slide 15 Pointers
CPSC 211 Data Structures & Implementations
c Teresa Leyk
° Slide 16 Pointers
CPSC 211 Data Structures & Implementations
c Teresa Leyk
° Slide 17 Pointers
CPSC 211 Data Structures & Implementations
return 0;
}
c Teresa Leyk
° Slide 18 Pointers