Week1 - Pointers
Week1 - Pointers
1
What is Data structure??
3
Outline
& address of
* value at address
a b p
1 2
balance = 3200;
balptr = &balance;
value = *balptr;
return 0;
}
Output
int *p;
double f;
// ...
p = &f; // ERROR
Assigning Values Through a Pointer
int *p;
int x = 12;
p=&x;
cout << x << endl;
//Assign a value to the location pointed to by p
*p = 101;
cout << x << endl; //what is value of x?
cout << *p << endl;
cout << p << endl;
cout << &x << endl;
Assigning values through a pointer
12
101
101
0012FF78
0012FF78
Assigning Values Through a Pointer
(*p)++; //increment value to the location pointed to by p
int main()
{
int *p, num;
p = #
*p = 454;
return 0;
} //Output?
Assigning Values Through a Pointer
.
pointer
what is pointed to
a value of
some type
Some Uses of Pointer Variables
linked lists
"lifetime" of a variable
is a run-time concept
period of time during which a variable has memory
space associated with it
begins when space is allocated
ends when space is de-allocated
three categories of "lifetime"
static - start to end of program execution
automatic (stack) - start to end of declaring function's
execution
heap - starts with "new"; ends when:
C++: "delete" statement executed
Data memory model
some examples
int * intPointer;
intPointer and
float * floatPointer;
floatPointer are
automatic
variables
Assigning a value to a pointer
variable
the value of a pointer variable is a memory address
assign address of an existing variable
int number;
intPointer = &number;
heap memory
intPointer
?
Dereferencing
heap variables do not have a name of their own
are anonymous variables
*intPointer refers to the value pointed to by intPointer
intPointer is the finger; *intPointer is the moon
what happens?
intPointer = 36;
*intPointer
*intPointer = 36;
(*intPointer)++; intPointer
cout << *intPointer;
intPointer = null;
Returning Heap Space
done by using the delete statement
syntax
delete <pointer variable>;
example
float * fPointer = new float;
cin >> (*fPointer);
------
delete fPointer;
Pointers - Summary
a pointer variable holds a memory address and can
be used to create and access dynamic variables
dynamic (heap) variables are explicitly created at
run-time (using new)
memory for dynamic variables is allocated in an area
of memory called the heap
space used for dynamic variables needs to be freed
(using delete) to avoid memory leaks
Dynamic Allocation
What is wrong here?
int *x2ptr;
*x2ptr=65;
Dynamic Allocation
int *xptr = new int;
*xptr = 73;
int *x2ptr;
x2ptr = xptr;
*x2ptr = 65;
Dynamic Allocation
//What is wrong here?
int *xptr = new int;
*xptr = 73;
delete myptr;
list[9]
Arrays
Arrays offer convenient means of grouping
together several related variables
One- dimensional arrays
type var_name[size]
int sample[10];
double d[30];
char ch[100];
Arrays in Data Structure
Examples:
double taxrate[3] = {0.15, 0.25, 0.3};
char word[] = “hello”;
char list[5] = {‘h’,’e’,’l’,’l’,’o’};
//list of characters, not a string
double vector[100] = {0.0}; //assigns zero to all 100 elements
Initializing Arrays
float f[5]={0.0, 1.0, 2.0, 3.0, 4.0};
// initializes f[0] to 0.0, f[1] to 1.0…f[4] to 4.0
Or
float f[5]; f[0]=0.0; f[1]=1.0; …
#include <iostream.h>
int main()
{
int t, sample[10]; // this reserves 10 integer
elements
return 0;
}
Initializing Arrays
int a[5];
Now an indexed variable like a[1] can be used anywhere that a variable of
type int can be used
int a[5];
cin >> a[4] >> a[2];
cout << a[2] << “ “ << a[4] << endl;
int student = 2;
a[student] = 99;
cout << a[student] << “ “ << a[2] << endl;
int main()
{
const int SIZE = 5;
int i, score[SIZE], max_score;
cout << “The highest score is: “ << max_score << endl
<< “Scores & their difference from highest: \n”;
• Total bytes =
number of bytes in type * number of elements
int size;
cin >> size;
int myarray[size];
// ...
a = b; // error -- illegal
/*So how do we make the contents of one array
same as the other?*/
Assigning One Array to
Another
int a[5], b[5];
// ...
a = b; // error – illegal
int main()
{
int b[ARRAY_SIZE];
return 0;
}
Assigning Values to an Array
•Example
int list[10];
int count;
return 1;
}
Sorting an Array
• Lots of applications require sorting
• Algorithm for sorting
int main(void) Sorting an
{
const int SIZE = 10; Array
double darray[SIZE] = {34, 5.6, 0.9, 345.7, 54.1, 23.5, 2.5, 6.78, 12.4, 13.9};
int i, j;
row[0]
row[1]
row[2]
in memory
row1 row2 row3
Accessing Array Elements
• int matrix[3][4];
– matrix has 12 integer elements
– matrix[0][0] element in first row, first column
– matrix[2][3] element in last row, last column
Two Dimensional Arrays
int main()
{
const int ROWS = 3;
const int COLS = 4;
int i, j, num[ROWS][COLS];
int multidim[4][10][3];
entry
34 45 15 ---------- 36
0 1 2 ----------- 30
delete []ptr;
Array of Pointers
const int RSIZE = 4;
A two dimensional
const int CSIZE = 6; array of
int * iarray[RSIZE]; RSIZE*CSIZE