4. Pointers
•A pointer is the memory address of a variable.
•If a variable holds numbers of Bytes then its address in memory is the address of the first byte !
•Pointer variable declarations : SYNTAX Type_Name *Variable_Name1, *Variable_Name2,. . .; EXAMPLE: double *pointer1, *pointer2;
5. Pointers
•A pointer is the memory address of a variable.
•If a variable holds numbers of Bytes then its address in memory is the address of the first byte !
•Pointer variable declarations : SYNTAX Type_Name *Variable_Name1, *Variable_Name2,. . .; EXAMPLE: double *pointer1, *pointer2;
Asterisk sign
6. Pointers
•void manipulatePointer(int *p); //Accepted but not as nice. int* p1, *p2; //Accepted but dangerous.
•void manipulatePointer(int* p); int *p1, *p2;
11. Pointers
•MyType *p; p = new MyType;
•MyType *mtPtr; mtPtr = new MyType(32.0, 17); // calls MyType(double, int);
constructor
•If the type is a class type :
12. Pointers
•MyType *p; p = new MyType;
•MyType *mtPtr; mtPtr = new MyType(32.0, 17); // calls MyType(double, int);
•If the type is a class type :
•int *n; n = new int(17); // initializes *n to 17
•nonclass types
15. Pointers
int *p; p = new int; if (p == NULL) { cout << "Error: Insufficient memory.n"; exit(1); } delete p; //If new succeeded, the program continues from here.
16. Pointers
int *p; p = new int; if (p == NULL) { cout << "Error: Insufficient memory.n"; exit(1); } delete p; //If new succeeded, the program continues from here.
Memory ensuring
17. Pointers
int *p;
p = new int;
if (p == NULL)
{
cout << "Error: Insufficient memory.n";
exit(1);
}
delete p;
//If new succeeded, the program continues from here.
Free the memory location
22. typedef
•You can use typedef to define an alias for any type name or definition.
typedef double Kilometers;
………………… Kilometers distance;
•You can define a pointer type name so that pointer variables can be declared like other variables without the need to place an asterisk in front of each pointer variable.
………………… IntPtr p1,p2,p3;
typedef int* IntPtr;
23. typedef
•You can use typedef to define an alias for any type name or definition.
typedef double Kilometers;
………………… Kilometers distance;
•You can define a pointer type name so that pointer variables can be declared like other variables without the need to place an asterisk in front of each pointer variable.
………………… IntPtr p1,p2,p3;
typedef int* IntPtr;
25. ARRAYS & POINTERS
•The Array name is a pointer to the first of array , so you can use a pointer instead of array name ( but in the opposite way can’t )
29. Dynamic array
•Determine memory places as we need !
•The User would put its dimension from runtime .
Type *p;
p=new Type[n];
30. Dynamic array
•Determine memory places as we need !
•The User would put its dimension from runtime .
Type *p;
p=new Type[n];
Double , float , char …
31. Dynamic array
•Determine memory places as we need !
•The User would put its dimension from runtime .
Type *p; p=new Type[n];
It could be by « User »
32. Dynamic array
•Determine memory places as we need !
•The User would put its dimension from runtime .
Type *p; p=new Type[n];
It could be by « User »
So it’s Dynamic !!!
38. POINTER ARITHMETIC
•*(p + i) == p[i] *(p++) == p[i++] *(++p ) == p[++i] and the same if “ - “
•subtract tow pointers(the same type ) returns the number of the same type elements !
39. POINTER ARITHMETIC
#include <iostream> using std::cout; using std::endl; int main( ) { int a[] = {1, 2, 3, 4, 5}; int* b; b = a; for (int i = 0; i < 5; i++) cout << *(b+i) << " "; return 0 ; } /* The above is equivalent to the following: for (int i = 0; i < arraySize; i++) cout << b[i] << " "; */
41. Multi dimensional dynamic array
•It’s an Array of Array !
•To make an Array n * m
•We make an (m) array of pointers from the type ( T ) by “new”
•then from each pointer we make an (n) array of type ( T ) by “new”
42. Multi dimensional dynamic array
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
typedef int* IntArrayPtr;
int main( )
{
int d1, d2;
cout << "Enter the row and column dimensions of the array:n";
cin >> d1 >> d2;
IntArrayPtr *m = new IntArrayPtr[d1];
int i, j;
for (i = 0; i < d1; i++)
m[i] = new int[d2];
//m is now a d1-by-d2 array.
cout << "Enter " << d1 << " rows of "
<< d2 << " integers each:n";
for (i = 0; i < d1; i++)
for (j = 0; j < d2; j++)
cin >> m[i][j];
cout << "Echoing the two-dimensional array:n";
for (i = 0; i < d1; i++)
{
for (j = 0; j < d2; j++)
cout << m[i][j] << " ";
cout << endl;
}
for (i = 0; i < d1; i++)
delete[] m[i];
delete[] m;
return 0;
}
43. Multi dimensional dynamic array
#include <iostream> using std::cin; using std::cout; using std::endl; typedef int* IntArrayPtr; int main( ) { int d1, d2; cout << "Enter the row and column dimensions of the array:n"; cin >> d1 >> d2; IntArrayPtr *m = new IntArrayPtr[d1]; int i, j; for (i = 0; i < d1; i++) m[i] = new int[d2]; //m is now a d1-by-d2 array. cout << "Enter " << d1 << " rows of " << d2 << " integers each:n"; for (i = 0; i < d1; i++) for (j = 0; j < d2; j++) cin >> m[i][j]; cout << "Echoing the two-dimensional array:n"; for (i = 0; i < d1; i++) { for (j = 0; j < d2; j++) cout << m[i][j] << " "; cout << endl; } for (i = 0; i < d1; i++) delete[] m[i]; delete[] m; return 0; }
44. Multi dimensional dynamic array
Enter the row and column dimensions of the array:
3 4
Enter 3 rows of 4 integers each:
1 2 3 4
5 6 7 8
9 0 1 2
Echoing the two-dimensional array:
1 2 3 4
5 6 7 8
9 0 1 2
58. Homework
• سؤال عممي :
ي ا رد بناء سمسمة خطية لمسيا ا رت لشركة ما حيث لكل سيارة
)رقم السيارة – تاريخ التسجيل (
• المطموب:
1 - كتابة اج ا رئية Insert لبناء السمسة بحيث يضاف المعطى
حسب الرقم بالمكان الصحيح
2 - كتابة اج ا رئية Print لطباعة بيانات السمسمة
3 - كتابة تابع يقوم بالبحث عن عنصر حسب الرقم
4 -كتابة تابع يقوم بمقارنة تاريخين
5 - طباعة السيا ا رت المسجمة قبل تاريخ معين بالاستعانة بالتابع
السابق