Académique Documents
Professionnel Documents
Culture Documents
Pointers
Henry Chia
hchia@comp.nus.edu.sg
Lecture Outline
Memory addresses
Pointers
Declaration and assignment
Dereference
Pointer comparison
Pointers in functions
Pointer parameters
Address as return value
Pointers to structures
Memory Addresses
When a program is executed, memory
locations are assigned to the variables.
Each of these memory locations has a
positive integer address that uniquely defines
the location.
When a variable is assigned a value, this
value is stored in the corresponding memory
location.
The specific addresses used for the variables
are determined each time that the program is
executed and may vary from one execution to
another.
CS1010 Lecture 7 p.3/31
Memory Addresses
For the following declaration:
int count = 1010;
count 1010
Address Operator
The address of a variable can be referenced using the
address operator &,
double x = 1.23;
printf("%lf stored at %p\n", x, &x);
Pointer Variable
The C language allows us to store the
address of a memory location in a special
type of variable called a pointer variable (or
simply pointer).
When a pointer is defined, the type of variable
to which it will point must also be defined.
Thus, a pointer defined to point to an integer
variable cannot also be used to point to a
floating-point variable.
Pointer Declaration
Consider the following declarations:
int count=1010, *ptr; // int count;
// int *ptr;
Pointer Declaration
The memory representation for the
declaration on the previous slide is as follows:
@100
Pointer Assignment
To specify that ptr should refer (or point) to
the variable count, assign the address of
count (&count) to ptr:
ptr = &count;
@100
<int> count
1010
@100
Pointer Dereference
Consider the following statements:
int count, *ptr;
ptr = &count;
*ptr = 1020;
printf("Var at %p stores %d\n", ptr, *ptr);
?
@100
// or ptr = 0;
CS1010 Lecture 7 p.15/31
Swapping Revisited
Consider the following swap function.
void swap(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
return;
}
Swapping Revisited
int main(void)
{
int x = 5, y = 2;
swap(x,y);
return 0;
}
main
x
swap(x,y);
}
swap
2
temp
6
swap(&x,&y);
ptrX
ptrY
When an address is passed to a function, draw a reference from the function parameter to the specified location.
CS1010 Lecture 7 p.20/31
6
swap(&x,&y);
-
ptrX
temp
ptrY
5
Pointers to Structures
Pointers variables can refer to structures.
struct Point
{
double x, y;
};
int main(void)
{
struct Point pt = {1,2},
*pPtr;
pPtr = &pt;
return 0;
}
pt
@500
1.0
2.0
@600
pPtr
@500
Lecture Summary
If you are new to pointers, devise and use
memory addresses to assist in program
traces; then progress on to using arrows.
Ensure type-consistency when assigning
pointers.
Addresses are passed to functions via
pass-by-address-value, i.e. a copy of the
address is passed to the pointer function
parameter.
Addresses and pointer dereferencing apply to
both primitive variables as well as to structure
variables.
CS1010 Lecture 7 p.31/31