0% found this document useful (0 votes)
50 views

Semester 1 2017 / 2018

- Memory addresses are assigned to variables when a program is executed. Each address uniquely identifies a memory location. - Variables store their values in these corresponding memory locations. The specific addresses may vary each time the program runs. - Pointers are variable types that store memory addresses. They must be declared with a type specifying the type of variable to which they will point. Pointers can be assigned memory addresses using the address operator &.

Uploaded by

Caroline Chua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Semester 1 2017 / 2018

- Memory addresses are assigned to variables when a program is executed. Each address uniquely identifies a memory location. - Variables store their values in these corresponding memory locations. The specific addresses may vary each time the program runs. - Pointers are variable types that store memory addresses. They must be declared with a type specifying the type of variable to which they will point. Pointers can be assigned memory addresses using the address operator &.

Uploaded by

Caroline Chua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Memory Addresses

 When a program is executed, memory locations are assigned


CS1010E Lecture 5 to the variables
 Each of these memory locations has a positive integer
C Pointers
address that uniquely defines the location
 When a variable is assigned a value, this value is stored in
Henry Chia ([email protected]) the corresponding memory location
 The specific addresses used for the variables are determined
each time that the program is executed and may vary from
Semester 1 2017 / 2018
one execution to another

1 / 20 3 / 20

Lecture Outline Memory Addresses

 Memory addresses  Consider the following declaration:


 Pointers
int count = 1010;
– Declaration and assignment
 Detailed memory representation:
– Dereference
– Pointer comparison @100
<int> count 1010

 We represent a memory address value with an @ sign


 Note that address values used are for illustration, and not
representative of the actual address allocation policy

2 / 20 4 / 20
Address Operator Declaring Pointer Variables

 The address of a variable can be referenced using the  The C language allows us to store the address of a memory
address operator & location in a special type of variable called a pointer
 This operator has always been used as part of a scanf variable (or simply pointer)
 When a pointer is declared, the type of variable to which it
double x; will point must also be specified
declaration
scanf("%lf", &x);
 When reading an input, scanf needs to know the location of type * varId ;

the memory associated with the variable, say x = value

@200 ,
<double> x 1.23
 C uses an asterisk in declarations to indicate that the
variable is a pointer variable

5 / 20 7 / 20

Address Operator Declaring Pointer Variables

 An address may be printed with a %d placeholder  Consider the following declarations:


double x; int count= 1010;
int *ptr;
scanf("%lf", &x);
printf("%f stored at %d\n", x, &x); or alternatively
int count= 1010, *ptr;
 Apart from debugging with printf, actual address values
are not used explicitly within a program as they are system  count is declared as an integer; ptr is declared as a pointer
dependent and may vary from one execution to another to an integer
 “int *ptr” is read from right to left as “declare ptr as a
pointer to a int”
 Identifier name is “ptr”; type is “int *”
 Although ptr can be initialized during declaration, you are
advised to use a separate assignment statement
6 / 20 8 / 20
Pointer Declaration Pointer Dereference

 The memory representation for the declaration on the  Consider the following statements:
previous slide is as follows: int count, *ptr;
@100 ptr = &count;
<int> count 1010 *ptr = 1020;
printf("Var at %d stores %d\n", ptr, *ptr);
@300
 Unlike declarations, the asterisk used in statements is a
<int *> ptr ?
dereferencing or indirection operator
 In the statement *ptr = 1020, the value 1020 is assigned
 Since ptr is a variable, to the “variable pointed to by ptr”
 The variable count now stores the value 1020
– it stores an unknown address value
 In the printf statement, *ptr is the “value pointed to by
– it is itself located at memory address @300
ptr”, which is the same as the value of count

9 / 20 11 / 20

Pointer Assignment More Pointer Assignments

 To specify that ptr should refer (or point) to the variable  Now consider the following declarations:
count, we can assign the address of count (&count) to int count = 1020, *ptr1, *ptr2;
the variable ptr:
ptr = &count; @300
<int *> ptr1 ?
 The memory representation is as follows:
@100 @300 @100
<int> count 1010 <int *> ptr @100 <int> count 1020
@400
<int *> ptr2 ?
 To avoid using memory address values, a convenient way is
to use an arrow to point to the desired variable

10 / 20 12 / 20
More Pointer Assignments Pointer Assignment Errors

 ptr1 = &count;  The following are some common errors when working with
– Assign the address of variable count to the variable ptr1 pointers, suppose
int y, *ptr1, *ptr2;
@300
<int *> ptr1 @100
– Changing the address of an integer variable:
@100 &y = ptr1;
<int> count 1020
– Assigning pointer with a non-address value:
@400 ptr1 = y;
<int *> ptr2 ? ptr1 = *ptr2;
– Assigning an address to an integer variable:
y = ptr2;
*ptr1 = ptr2;
13 / 20 15 / 20

More Pointer Assignments Pointer Comparisons and NULL

 ptr2 = ptr1;  To test whether two different pointers are pointing to the
same variable or not, we use relational operators == and !=
– Assign value of ptr1 to variable ptr2 to compare pointers
– A pointer can be assigned to another pointer of the
same type, i.e. several pointers can point to the same if (ptr1 == ptr2) {
printf("Both pointers point to the same variable\n");
location }
@300
 Other relational operators <, >, <=, >= more relevant in the
<int *> ptr1 @100
context of arrays
@100  A pointer can be assigned to or compared with the constant
<int> count 1020 NULL (which is the integer zero) defined in <stdio.h>
@400  To zero a pointer, assign a NULL value to indicate that the
<int *> ptr2 @100 pointer has no address
ptr = NULL; // or ptr = 0;
14 / 20 16 / 20
Pointer Exercises More pointers... (non-examinable)

 Give memory snapshots after each of these sets of  Can we declare a pointer variable that points to another
statements is executed: pointer variable? Yes, we can!
declaration
(a)
int a = 1, b = 2, *ptr; type varId ;

ptr = &b; * = value


a = *ptr;

(b)
,
int a = 1, b = 2, c = 5, *ptr;
ptr = &c; int count, *ptr1, **ptr2;
c = b;
a = *ptr; ptr1 = &count;
ptr2 = &ptr1;
**ptr2 = 1010;
17 / 20 19 / 20

Pointer Exercises Lecture Summary

 Given the following program fragment  Be able to declare pointers of the appropriate type
int i1, i2;
 Be able to assign pointers with appropriate address values
int *p1, *p2;  Be able to dereference a pointer to access the variable that it
i1 = 5; is pointing to
p1 = &i1;  Pointer variables are no different from the typical primitive
i2 = (*p1)/2 + 10;
p2 = p1; variable; assignment and comparison works the same way
 Always ensure type-consistency when assigning pointers
 Trace the code and give memory snapshots of the following:
 If you are new to pointers, devise and use memory addresses
– What is the value of i1 and i2? to assist in program traces first; then progress on to using
– What is the value of *p1 and *p2? arrows
– What is the value of &i1 and &i2?
– What is the value of p1 and p2?

18 / 20 20 / 20

You might also like