Documente Academic
Documente Profesional
Documente Cultură
MTS3023
Data Structure
Arrays, Pointers and Struct
+
ARRAYS
+
What is an array?
Option 1: Option 3:
int myarray[ 5 ]; int myarray[5];
for ( int i = 0; i < 5; i++ ) myarray[0] = 5;
{ myarray[1] = 8;
myarray[ i ] = 0; myarray[2] = 2;
} myarray[3] = 1;
myarray[4] = 9;
Option 2
int myarray[ 5 ] = { 2, 7, 4, 8, 5 };
int main()
{
int i;
int list[ 10 ] = { 2, 7, 4, 8, 5, 4, 9, 7, 6, 3 }; 2
for ( i = 0; i < 5; i++ ) 7
{ 4
8
cout<<list[ i ]<<endl; 5
4
} 9
7
return 0; 6
} 3
+
2 Dimensional arrays
#include <iostream>
using namespace std;
5 4 5 5
int main() 3 7 4 7
{ 1 6 8 4
int table[3][4];
return 0;
}
+
How to assign values?
#include <iostream>
using namespace std;
int main()
{
int table[3][4] = {{5,4,5,5},{3,7,4,7},{1,6,8,4}};
return 0;
}
5 4 5 5
3 7 4 7
1 6 8 4
+
How to access or display the values in
the 2D arrays ?
int table[3][4];
for (i= 0 ; i < 3 ; i++) //outer loop
{
for (j = 0; j < 4 ; j++) //inner loop
{
cin>>table[i][j];
}
}
+
POINTERS
+
Introduction
value 10 cout<< v ;
Meanwhile….
int v; name v
v= 10 address F001111E
value F001111E
p = &v; name p
address F005555E
cout<< p ;
cout<<*p ;
+
Let’s check…
#include<iostream.h>
using namespace std;
int main()
10
{
int v; v
int *p; F005555E
v = 10;
cout<<v<<endl;
cout<<&v<<endl;
p = &v;
cout<<p<<endl; What is the output ?
cout<<*p<<endl;
*p = 8;
cout<<v<<endl;
cout<<p<<endl;
cout<<*p<<endl;
return 0;
}
+
+
Remember:
Declaring a pointer:
Eg:
int *p ;
double *p ;
float *p ;
We can also assign a value like this:
Dynamic Memory
Memory is allocated at running time
+
Static vs. Dynamic Objects
new
delete
{ int* ptr;
int a[200]; ptr = new int[200];
… …
} delete [] ptr;
+ Object (variable) creation: New
Syntax
Example:
int* p = new int;
p
+Object (variable) destruction: Delete
Syntax
delete p;
storage pointed to by p is returned to free store and p is now
undefined
Example
int* p = new int;
*p = 10;
delete p;
p 10
+ Example
p
p = new int[100];
new
p
new
p = new int[n]; p
+
Memory Allocation Example
#include <iostream>
using namespace std;
void main()
{
int n;
cout << “How many students? “;
cin >> n;
int *grades = new int[n];
for(int i=0; i < n; i++){
int mark;
cout << “Input Grade for Student” << (i+1) << “ ? :”;
cin >> mark;
grades[i] = mark;
}
...
printMean( grades, n ); // call a function with dynamic
array
...
}
+
Freeing (or deleting) Memory
+ A Simple Dynamic List Example
#include <iostream>
using namespace std;
int main ()
{
int i,n;
int * p;
cout << "How many numbers would you like to type? ";
cin >> i;
p= new int[i];
if (p == 0)
cout << "Error: memory could not be allocated";
else
{
for (n=0; n<i; n++)
{
cout << "Enter number: ";
cin >> p[n];
}
cout << "You have entered: ";
for (n=0; n<i; n++)
cout << p[n] << ", ";
delete[] p;
}
return 0;
}
+
Dangling Pointer Problem
A
0 1 2 3 4
B
delete [] A;
B[0] = 1; // illegal!
A —
?
B
+
Memory Leak Problem
int *A = new int [5];
for(int i=0; i<5; i++)
A[i] = i;
A 0 1 22 3 4
A 0 1 2 3 4
— — — — —
+
Dynamic 2D Array
table 32 18 12 24
table[0]
table[1] 13 11 16 12 42 19 14
A dynamic array is an table[2]
array of pointers to 22
save space when not table[3]
all rows of the array table[4] 13 13 14
are full. table[5]
int **table;
11 18
table = new int*[6];
…
table[0] = new int[4];
table[1] = new int[7];
table[2] = new int[1];
table[3] = new int[3];
table[4] = new int[2];
table[5] = NULL;
+
Memory Deallocation
Student student1 ;
Student student2 ;
Let’s compare:
int age ;
Student student1 ;
+
Let’s learn more - struct
Memory management
age
Simple data type F005555E
int age ;
#include<iostream.h>
using namespace std;
struct Student {
char id[14];
char name[20];
int age;
};
int main()
{
int age;
Student student1;
age = 19;
strcpy(student1.id,"D201010100");
strcpy(student1.name,"ida");
student1.age = 19;
cout<<age<<endl;
cout<<student1.id<<student1.name<<student1.age<<endl;
return 0;
}
+
Simple Exercise
http://www.youtube.com/watch?v=XEzM77ICfBU
http://www.youtube.com/watch?v=RwDttcHRYHA&feature=related
http://www.youtube.com/watch?v=0k23xe3c4es&feature=mfu_in_ord
er&list=UL