Вы находитесь на странице: 1из 36

ITec 2042

Fundamentals of Programming II

Chapter Two
Pointers
1
October 2013
Introduction to Pointers
o When we declare a variable some memory is
allocated for it.
o The memory location can be referenced
through the identifier “i”. Thus, we have two
properties for any variable:
- its address
- its data value
o The address of the variable can be accessed
through the referencing operator “&”.
o “&i” gives the memory location where the data
2
value for “i” is stored.
o Addresses displayed in HEXADECIMAL
Introduction to Pointers
o A pointer variable is one that stores an
address.
o We can declare pointers as follows
int* p;
o This means that p stores the address of a
variable of type int.

3
Introduction to Pointers
o Q: Why is it important to declare the type of the
variable that a pointer points to? Aren’t all
addresses of the same length?
o A: It’s true that all addresses are of the same
length, however when we perform an operation of
the type “p++” where “p” is a pointer variable, for
this operation to make sense the compiler needs to
know the data type of the variable “p” points to. If
“p” is a character pointer then “p++” will
increment “p” by one byte (typically), if “p” were
an integer pointer its value on “p++” would be
4
incremented by 2 bytes (typically).
Introduction to Pointers
o Summary of what was learnt so far:
- Pointer is a data type that stores addresses, it is
declared as follows:
int* a; char* p; etc.
- The value stored in a pointer p can be accessed
through the dereferencing operator “*”.
- The address of a memory location of a variable can
be accessed through the reference operator “&”.
- Pointer arithmetic: only addition and subtraction
are allowed.
5
Example
#include <iostream.h>
value FFF0 56.47
FFF1
void main( )
{ FFF2

int data = 100; FFF3

float value = 56.47; data FFF4 100


cout << data << &data << endl; FFF5
cout << value << &value << FFF6
endl;
}

Output:
6
100 FFF4
56.47 FFF0
Pointer Variables
o The pointer data type
- A data type for containing an address rather
than a data value
- Integer, similar to int
- Size is the number of bytes in which the
target computer stores a memory address
- Provides indirect access to values

7
Declaring pointers
o A pointer variable is declared by:
dataType *pointerVarName;
- The pointer variable pointerVarName is
used to point to a value of type dataType
- The * before the pointerVarName indicates
that this is a pointer variable, not a regular
variable
- The * is not a part of the pointer variable
name 8
Declaring pointers (contd…)
o Example
int *ptr1;
ptr1 is a pointer to an int value i.e., it can have
the address of the memory location allocated to an
int value

int i1=26;
int* ptr1 = &i1;
string s1 = "Hello";
string* ptr2 = &s1;
void* ptr2a = &s1;
9
float* ptr3; // bad
float* ptr4=0; // better
Declaring pointers (contd…)
o Whitespace doesn’t matter and each of the
following will declare ptr as a pointer (to
a float) variable and data as a float
variable

float *ptr, data;


float* ptr, data;
float (*ptr), data;
float data, *ptr;
10
Assignment of pointers
 A pointer variable has to be assigned a valid
memory address before it can be used in the
program
 Example:
float data = 50.8;
float *ptr;
ptr = &data;
 This will assign the address of the memory location
allocated for the floating point variable data to the
pointer variable ptr. This is OK, since the variable
data has already been allocated some memory 11
space having a valid address
Assignment of pointers (cont…)

FFF0
FFF1
FFF2
FFF3
data FFF4 50.8
float data = 50.8;
FFF5
float *ptr; FFF6
ptr = &data;

12
Assignment of pointers (cont…)

ptr FFF0
FFF1
FFF2
FFF3
data FFF4 50.8
float data = 50.8;
FFF5
float *ptr; FFF6
ptr = &data;

13
Assignment of pointers (cont…)

ptr FFF0 FFF4


FFF1
FFF2
FFF3
data FFF4 50.8
float data = 50.8;
FFF5
float *ptr; FFF6
ptr = &data;

14
Assignment of pointers (cont…)

Don’t try to assign a specific integer value to


a pointer variable since it can be disastrous
float *ptr;
ptr = 120;
• You cannot assign the address of one type of
int data = 50;
float *ptr;
ptr = &data; 15
Initializing Pointers
o A pointer can be initialized during
declaration by assigning it the address
of an existing variable
float data = 50.8;
float *ptr = &data;
o If a pointer is not initialized during
declaration, it is wise to give it a NULL
(0) value
int *ip = 0; 16

float *fp = NULL;


The Null Pointer
o The NULL pointer is a valid address
for any data type.
o But NULL is not memory address 0.
o It is an error to dereference a pointer
whose value is NULL.
o Such an error may cause your program
to crash, or behave erratically
o It is the programmer’s job to check for
17
this.
Dereferencing
o Dereferencing- using a pointer variable
to access the value stored at the location
pointed by the variable
o Provide indirect access to values and
also called indirection
o Done by using the dereferencing operator
* in front of a pointer variable
o Unary operator
o Highest precedence 18
Dereferencing (cont…)
o Example:
float data = 50.8;
float *ptr;
ptr = &data;
cout << *ptr;
o Once the pointer variable ptr has been
declared, *ptr represents the value
pointed to by ptr (or the value located at
the address specified by ptr) and may
be treated like any other variable of 19

float type
Dereferencing (cont…)
o The dereferencing operator * can be
used in assignments.
*ptr = 200;
o Make sure that ptr has been properly
initialized

 WARNING!
Be careful to initialise pointers before
trying to use them 20
Dereferencing Example
#include <iostream.h>
void main() ptr FFF0 FFF4
FFF1
{
FFF2
float data = 50.8;
FFF3
float *ptr; data FFF4 50.8
ptr = &data; FFF5
cout << ptr << *ptr FFF6
<< endl;
*ptr = 27.4;
cout << *ptr << endl;
cout << data << endl;
21
}
Output:
Dereferencing Example (cont...)
#include <iostream.h>
void main() ptr FFF0 FFF4
FFF1
{
FFF2
float data = 50.8;
FFF3
float *ptr; data FFF4 50.8
ptr = &data; FFF5
cout << ptr << *ptr FFF6
<< endl;
*ptr = 27.4;
cout << *ptr << endl;
cout << data << endl;
22
}
Output:
Dereferencing Example (cont...)
#include <iostream.h>
void main() ptr FFF0 FFF4
FFF1
{
FFF2
float data = 50.8;
FFF3
float *ptr; data FFF4 27.4
ptr = &data; FFF5
cout << ptr << *ptr FFF6
<< endl;
*ptr = 27.4;
cout << *ptr << endl;
cout << data << endl;
23
}
Output:
Dereferencing Example (cont...)
#include <iostream.h>
void main() ptr FFF0 FFF4
FFF1
{
FFF2
float data = 50.8;
FFF3
float *ptr; data FFF4 27.4
ptr = &data; FFF5
cout << ptr << *ptr FFF6
<< endl;
*ptr = 27.4;
cout << *ptr << endl;
cout << data << endl;
24
}
Output:
Dereferencing Example (cont...)
#include <iostream.h>
void main() ptr FFF0 FFF4
FFF1
{
FFF2
float data = 50.8;
FFF3
float *ptr; data FFF4 27.4
ptr = &data; FFF5
cout << ptr << *ptr FFF6
<< endl;
*ptr = 27.4;
cout << *ptr << endl;
cout << data << endl;
25
}
Output:
Operations on Pointer Variables
o Assignment – the value of one pointer
variable can be assigned to another pointer
variable of the same type
o Relational operations - two pointer
variables of the same type can be compared
for equality, and so on
o Some limited arithmetic operations
o integer values can be added to and
subtracted from a pointer variable
o value of one pointer variable can be 26
subtracted from another pointer
variable
Pointers summary

o ‘size of’ operator sizeof(i)


o ‘address of’ operator &i
o Pointer data types int* p1
o Value of (dereference) *p1
o Null pointers p1=0

27
Constant Pointers
o Data we don’t want to change in the program
is declared as a constant
const float pi = 3.141592;
const int max = 10;
o If declare pointer as a constant we cannot
change the address stored-but we can change
the information at that address
int i1=26, i2=28;
const int* ptr = &i1;
ptr = &i2; // error b/c it already
declared as const i1 28
i1=27;
*ptr = 27;
Pointers & Arrays
o The identifier of an array actually a pointer that
holds the address of the first element of the array.
o Therefore if you have two declarations as follows:
 “int a[10];” “int* p;” then the assignment “p =
a;” is perfectly valid
 Also “*(a+4)” and “a[4]” are equivalent as are
“*(p+4)” and “p[4]”.
 The only difference between the two is that we
can change the value of “p” to any integer
variable address whereas “a” will always point
29
to the integer array of length 10 defined.
Pointers & Arrays
o An array variable is really just a (const)
pointer to the first value of the array,

int a[10] =
{2,4,8,16,32,64,128,256,512,1024};

cout << &a[0]; // 0x...


cout << a; // Ox...
30
cout << a[0]; // 2
cout << *a; // 2
Character Pointers, Arrays & Strings
o String- is a character array that is ‘\0’
terminated.
o E.g. “Hello”
o Character array- is an array of characters,
not necessarily ‘\0’ terminated.
o E.g. char test[4] = {‘a’, ‘b’, ‘c’, ‘d’}; <this char
array is not zero terminated>

o Character pointer- a pointer to the address


of a character variable. 31
o E.g. char* a; <this pointer is not initialized>
Character Pointers, Arrays & Strings
o How do we initialize a Character
pointer?
o Initialize it to NULL. char* a = NULL;
o Let it point to a character array.
o char* a; char b[100]; a = b;
o Initialize to a character string.
o char* a=“Hello”; a pointer to the memory
location where ‘H’ is stored.
o Here “a” can be viewed as a character array
32
of size 6, the only difference being that a can
be reassigned another memory location.
Examples
o char* a = “Hello”;
o a -> gives address of ‘H’
o *a -> gives ‘H’
o a[0] -> gives ‘H’
o a++ -> gives address of ‘e’
o *a++ -> gives ‘e’
o a = &b; where b is another char variable
is perfectly LEGAL. However “char a[100];”
33
“a =&b;” where b is another char variable
is ILLEGAL.
Pointer Arithmetic
o When we add one to the value of a pointer,
we actually add the size of the data-type it
points to,

int i;
int* p = &i;
int* q = p+1;
cout << p; // eg 0x0063FDF8
cout << q; // eg Ox0063FDFC
cout << sizeof(i); // 4 34
cout << q - p; // 1
Pointers to Functions
o A function is also stored in memory
o Can have a pointer to a function
int compare(int a, int b);
int (* p1) (int, int) =
&compare;
o Why the extra brackets?
- What about functions that return pointers?
int* compare(int a, int b);
- a function returning a pointer?
35
- or a pointer to a function?
Thank You !!!
Any Questions
??
36

Вам также может понравиться