SlideShare una empresa de Scribd logo
1 de 61
Descargar para leer sin conexión
Kinan keshkeh 
IT Engineering-Damascus University 
3rd year 
Summer course- 2014 
2 bytes team
Welcome !
Pointers !
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;
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
Pointers 
•void manipulatePointer(int *p); //Accepted but not as nice. int* p1, *p2; //Accepted but dangerous. 
•void manipulatePointer(int* p); int *p1, *p2;
Pointers 
•int* findOtherPointer(int* p); 
Pointers can be Returned type
Pointers 
42 
42 
v1 = 0; 
p1 = &v1; 
*p1 = 42; 
cout << v1 << endl; 
cout << *p1 << endl;
Pointers 
42 42 
v1 = 0; 
p1 = &v1; 
*p1 = 42; 
cout << v1 << endl; 
cout << *p1 << endl;
Pointers 
•See the difference
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 :
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
Pointers
Pointers 
•Explanation:
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.
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
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
Pointers
Pointers
Pointers 
Output:
“Typedef“
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;
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;
ARRAYS & POINTERS !
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 )
ARRAYS & POINTERS
ARRAYS & POINTERS 
Output :
Dynamic array !
Dynamic array 
•Determine memory places as we need ! 
•The User would put its dimension from runtime . 
Type *p; 
p=new Type[n];
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 …
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 »
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 !!!
Dynamic array
Dynamic array
Dynamic array 
delete a [] ; « Illegal »
Dynamic array
POINTER ARITHMETIC !
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 !
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] << " "; */
Multi dimensional dynamic array !
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”
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; 
}
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; }
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
ARRAYS & classes !
ARRAYS & classes 
struct Record 
{ 
int number; 
char grade; 
}; 
Record *p; 
p = new Record; 
p->number = 2001; 
p->grade = ’A’; // <===========> (*p).grade=‘A’ 
•The operator “ -> “ :
ARRAYS & classes 
class Sample 
{ 
public: 
... 
void showStuff( ) const; 
... 
private: 
int stuff; 
... 
}; 
•THE this POINTER :
ARRAYS & classes 
void Sample::showStuff( ) const { cout << stuff; } //Not good style, but this illustrates the this pointer void Sample::showStuff( ) { cout << this->stuff; } 
•THE this POINTER :
ARRAYS & classes 
•THE this POINTER : 
It is useful when you have a collision with another variable at the same namespace !
ARRAYS & classes 
#include <iostream> 
#include <cstdlib> 
using namespace std; 
class DayOfYear{ 
public: 
void input( ); 
void output( ); 
void set(int month, int day); 
void set(int month); 
int getMonthNumber( ); 
int getDay( ); 
private: 
int month; 
int day; 
}; 
void DayOfYear::set(int month, int day) 
{ 
if ((month >= 1) && (month <= 12)) 
this->month = month; 
else{ 
cerr << "Illegal month value! Program aborted.n"; 
exit(1); 
} 
if ((day >= 1) && (day <= 31)) 
this->day = day; 
else{ 
cout << "Illegal day value! Program aborted.n"; 
exit(1); 
} 
}
Code discussion !
Code discussion
Code discussion
Code discussion
Code discussion
Code discussion
Code discussion 
Output Example :
Homework 
• سؤال عممي : 
ي ا رد بناء سمسمة خطية لمسيا ا رت لشركة ما حيث لكل سيارة 
)رقم السيارة – تاريخ التسجيل ( 
• المطموب: 
1 - كتابة اج ا رئية Insert لبناء السمسة بحيث يضاف المعطى 
حسب الرقم بالمكان الصحيح 
2 - كتابة اج ا رئية Print لطباعة بيانات السمسمة 
3 - كتابة تابع يقوم بالبحث عن عنصر حسب الرقم 
4 -كتابة تابع يقوم بمقارنة تاريخين 
5 - طباعة السيا ا رت المسجمة قبل تاريخ معين بالاستعانة بالتابع 
السابق
That’s for today 
That’s for today guys !!
That’s for today 
Go and Play ! 
Bye Bye !
2 bytes team 
Group : group link 
Mobile phone- Kinan : 0994385748 
Facebook account : kinan’s account 
2 bytes team

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

C++11
C++11C++11
C++11
 
What's New in C++ 11/14?
What's New in C++ 11/14?What's New in C++ 11/14?
What's New in C++ 11/14?
 
C++ references
C++ referencesC++ references
C++ references
 
Pointers
PointersPointers
Pointers
 
Smart Pointers in C++
Smart Pointers in C++Smart Pointers in C++
Smart Pointers in C++
 
06.Loops
06.Loops06.Loops
06.Loops
 
Array notes
Array notesArray notes
Array notes
 
13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text Processing
 
Smart pointers
Smart pointersSmart pointers
Smart pointers
 
C++11: Rvalue References, Move Semantics, Perfect Forwarding
C++11: Rvalue References, Move Semantics, Perfect ForwardingC++11: Rvalue References, Move Semantics, Perfect Forwarding
C++11: Rvalue References, Move Semantics, Perfect Forwarding
 
C++11 smart pointer
C++11 smart pointerC++11 smart pointer
C++11 smart pointer
 
C++ L09-Classes Part2
C++ L09-Classes Part2C++ L09-Classes Part2
C++ L09-Classes Part2
 
C++11 concurrency
C++11 concurrencyC++11 concurrency
C++11 concurrency
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
 
C++ L11-Polymorphism
C++ L11-PolymorphismC++ L11-Polymorphism
C++ L11-Polymorphism
 
C++ L02-Conversion+enum+Operators
C++ L02-Conversion+enum+OperatorsC++ L02-Conversion+enum+Operators
C++ L02-Conversion+enum+Operators
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
friends functionToshu
friends functionToshufriends functionToshu
friends functionToshu
 

Destacado

2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graphkinan keshkeh
 
10 Little Tricks to Get Your Class’s Attention (and Hold It)
10 Little Tricks to Get Your  Class’s Attention (and Hold It)10 Little Tricks to Get Your  Class’s Attention (and Hold It)
10 Little Tricks to Get Your Class’s Attention (and Hold It)kinan keshkeh
 
2 BytesC++ course_2014_c13_ templates
2 BytesC++ course_2014_c13_ templates2 BytesC++ course_2014_c13_ templates
2 BytesC++ course_2014_c13_ templateskinan keshkeh
 
2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers2CPP06 - Arrays and Pointers
2CPP06 - Arrays and PointersMichael Heron
 
Authorship and Autership
Authorship and AutershipAuthorship and Autership
Authorship and AutershipMichael Heron
 
ACCESS: A Technical Framework for Adaptive Accessibility Support
ACCESS:  A Technical Framework for Adaptive Accessibility SupportACCESS:  A Technical Framework for Adaptive Accessibility Support
ACCESS: A Technical Framework for Adaptive Accessibility SupportMichael Heron
 
Pointers in c
Pointers in cPointers in c
Pointers in cMohd Arif
 
Ise rt c2_s14_nour_40714
Ise rt c2_s14_nour_40714Ise rt c2_s14_nour_40714
Ise rt c2_s14_nour_40714Alaa Bar Avi
 
أخلاقيات العالم الافتراضي نهائي
أخلاقيات العالم الافتراضي نهائيأخلاقيات العالم الافتراضي نهائي
أخلاقيات العالم الافتراضي نهائيAlaa Bar Avi
 
Accessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkAccessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkMichael Heron
 
Musings on misconduct
Musings on misconductMusings on misconduct
Musings on misconductMichael Heron
 
تصميم مواقع الشرق الأوسط
تصميم مواقع الشرق الأوسطتصميم مواقع الشرق الأوسط
تصميم مواقع الشرق الأوسطAlaa Bar Avi
 
النشرالإلكتروني
النشرالإلكترونيالنشرالإلكتروني
النشرالإلكترونيAlaa Bar Avi
 
مبادئ التحرير الإعلامي
مبادئ التحرير الإعلاميمبادئ التحرير الإعلامي
مبادئ التحرير الإعلاميAlaa Bar Avi
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++LPU
 
التحرير الإخباري
التحرير الإخباريالتحرير الإخباري
التحرير الإخباريAlaa Bar Avi
 

Destacado (17)

2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph
 
10 Little Tricks to Get Your Class’s Attention (and Hold It)
10 Little Tricks to Get Your  Class’s Attention (and Hold It)10 Little Tricks to Get Your  Class’s Attention (and Hold It)
10 Little Tricks to Get Your Class’s Attention (and Hold It)
 
2 BytesC++ course_2014_c13_ templates
2 BytesC++ course_2014_c13_ templates2 BytesC++ course_2014_c13_ templates
2 BytesC++ course_2014_c13_ templates
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers
 
Authorship and Autership
Authorship and AutershipAuthorship and Autership
Authorship and Autership
 
ACCESS: A Technical Framework for Adaptive Accessibility Support
ACCESS:  A Technical Framework for Adaptive Accessibility SupportACCESS:  A Technical Framework for Adaptive Accessibility Support
ACCESS: A Technical Framework for Adaptive Accessibility Support
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
Ise rt c2_s14_nour_40714
Ise rt c2_s14_nour_40714Ise rt c2_s14_nour_40714
Ise rt c2_s14_nour_40714
 
أخلاقيات العالم الافتراضي نهائي
أخلاقيات العالم الافتراضي نهائيأخلاقيات العالم الافتراضي نهائي
أخلاقيات العالم الافتراضي نهائي
 
Accessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkAccessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS Framework
 
Musings on misconduct
Musings on misconductMusings on misconduct
Musings on misconduct
 
تصميم مواقع الشرق الأوسط
تصميم مواقع الشرق الأوسطتصميم مواقع الشرق الأوسط
تصميم مواقع الشرق الأوسط
 
النشرالإلكتروني
النشرالإلكترونيالنشرالإلكتروني
النشرالإلكتروني
 
مبادئ التحرير الإعلامي
مبادئ التحرير الإعلاميمبادئ التحرير الإعلامي
مبادئ التحرير الإعلامي
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
التحرير الإخباري
التحرير الإخباريالتحرير الإخباري
التحرير الإخباري
 

Similar a Pointers and dynamic memory in C

Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Languagemadan reddy
 
C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementC Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementSreedhar Chowdam
 
2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++kinan keshkeh
 
C++ process new
C++ process newC++ process new
C++ process new敬倫 林
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloadingkinan keshkeh
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++NUST Stuff
 
C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8Christian Nagel
 
ch03-parameters-objects.ppt
ch03-parameters-objects.pptch03-parameters-objects.ppt
ch03-parameters-objects.pptMahyuddin8
 
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handlingBsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handlingRai University
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - ReferenceMohammed Sikander
 
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handlingRai University
 
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingMca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingRai University
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingMeghaj Mallick
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingRai University
 

Similar a Pointers and dynamic memory in C (20)

Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementC Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory management
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++
 
C++ process new
C++ process newC++ process new
C++ process new
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
 
C
CC
C
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++
 
C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8
 
Pointers
PointersPointers
Pointers
 
ch03-parameters-objects.ppt
ch03-parameters-objects.pptch03-parameters-objects.ppt
ch03-parameters-objects.ppt
 
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handlingBsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
Object Oriented Programming using C++ - Part 4
Object Oriented Programming using C++ - Part 4Object Oriented Programming using C++ - Part 4
Object Oriented Programming using C++ - Part 4
 
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
 
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingMca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
 
Interesting facts on c
Interesting facts on cInteresting facts on c
Interesting facts on c
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
 

Más de kinan keshkeh

Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods kinan keshkeh
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptkinan keshkeh
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptkinan keshkeh
 
GeneticAlgorithms_AND_CuttingWoodAlgorithm
GeneticAlgorithms_AND_CuttingWoodAlgorithm  GeneticAlgorithms_AND_CuttingWoodAlgorithm
GeneticAlgorithms_AND_CuttingWoodAlgorithm kinan keshkeh
 
Algorithm in discovering and correcting words errors in a dictionary or any w...
Algorithm in discovering and correcting words errors in a dictionary or any w...Algorithm in discovering and correcting words errors in a dictionary or any w...
Algorithm in discovering and correcting words errors in a dictionary or any w...kinan keshkeh
 
2Bytesprog2 course_2014_c8_units
2Bytesprog2 course_2014_c8_units2Bytesprog2 course_2014_c8_units
2Bytesprog2 course_2014_c8_unitskinan keshkeh
 
2Bytesprog2 course_2014_c7_double_lists
2Bytesprog2 course_2014_c7_double_lists2Bytesprog2 course_2014_c7_double_lists
2Bytesprog2 course_2014_c7_double_listskinan keshkeh
 
2Bytesprog2 course_2014_c6_single linked list
2Bytesprog2 course_2014_c6_single linked list2Bytesprog2 course_2014_c6_single linked list
2Bytesprog2 course_2014_c6_single linked listkinan keshkeh
 
2Bytesprog2 course_2014_c5_pointers
2Bytesprog2 course_2014_c5_pointers2Bytesprog2 course_2014_c5_pointers
2Bytesprog2 course_2014_c5_pointerskinan keshkeh
 
2Bytesprog2 course_2014_c4_binaryfiles
2Bytesprog2 course_2014_c4_binaryfiles2Bytesprog2 course_2014_c4_binaryfiles
2Bytesprog2 course_2014_c4_binaryfileskinan keshkeh
 
2Bytesprog2 course_2014_c3_txtfiles
2Bytesprog2 course_2014_c3_txtfiles2Bytesprog2 course_2014_c3_txtfiles
2Bytesprog2 course_2014_c3_txtfileskinan keshkeh
 
2Bytesprog2 course_2014_c2_records
2Bytesprog2 course_2014_c2_records2Bytesprog2 course_2014_c2_records
2Bytesprog2 course_2014_c2_recordskinan keshkeh
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_setskinan keshkeh
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_setskinan keshkeh
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_setskinan keshkeh
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_setskinan keshkeh
 
2 BytesC++ course_2014_c12_ polymorphism
2 BytesC++ course_2014_c12_ polymorphism2 BytesC++ course_2014_c12_ polymorphism
2 BytesC++ course_2014_c12_ polymorphismkinan keshkeh
 
2 BytesC++ course_2014_c11_ inheritance
2 BytesC++ course_2014_c11_ inheritance2 BytesC++ course_2014_c11_ inheritance
2 BytesC++ course_2014_c11_ inheritancekinan keshkeh
 
2 BytesC++ course_2014_c10_ separate compilation and namespaces
2 BytesC++ course_2014_c10_ separate compilation and namespaces 2 BytesC++ course_2014_c10_ separate compilation and namespaces
2 BytesC++ course_2014_c10_ separate compilation and namespaces kinan keshkeh
 
2 BytesC++ course_2014_c8_ strings
2 BytesC++ course_2014_c8_ strings 2 BytesC++ course_2014_c8_ strings
2 BytesC++ course_2014_c8_ strings kinan keshkeh
 

Más de kinan keshkeh (20)

Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop concept
 
Shapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop conceptShapes and calculate (area and contour) / C++ oop concept
Shapes and calculate (area and contour) / C++ oop concept
 
GeneticAlgorithms_AND_CuttingWoodAlgorithm
GeneticAlgorithms_AND_CuttingWoodAlgorithm  GeneticAlgorithms_AND_CuttingWoodAlgorithm
GeneticAlgorithms_AND_CuttingWoodAlgorithm
 
Algorithm in discovering and correcting words errors in a dictionary or any w...
Algorithm in discovering and correcting words errors in a dictionary or any w...Algorithm in discovering and correcting words errors in a dictionary or any w...
Algorithm in discovering and correcting words errors in a dictionary or any w...
 
2Bytesprog2 course_2014_c8_units
2Bytesprog2 course_2014_c8_units2Bytesprog2 course_2014_c8_units
2Bytesprog2 course_2014_c8_units
 
2Bytesprog2 course_2014_c7_double_lists
2Bytesprog2 course_2014_c7_double_lists2Bytesprog2 course_2014_c7_double_lists
2Bytesprog2 course_2014_c7_double_lists
 
2Bytesprog2 course_2014_c6_single linked list
2Bytesprog2 course_2014_c6_single linked list2Bytesprog2 course_2014_c6_single linked list
2Bytesprog2 course_2014_c6_single linked list
 
2Bytesprog2 course_2014_c5_pointers
2Bytesprog2 course_2014_c5_pointers2Bytesprog2 course_2014_c5_pointers
2Bytesprog2 course_2014_c5_pointers
 
2Bytesprog2 course_2014_c4_binaryfiles
2Bytesprog2 course_2014_c4_binaryfiles2Bytesprog2 course_2014_c4_binaryfiles
2Bytesprog2 course_2014_c4_binaryfiles
 
2Bytesprog2 course_2014_c3_txtfiles
2Bytesprog2 course_2014_c3_txtfiles2Bytesprog2 course_2014_c3_txtfiles
2Bytesprog2 course_2014_c3_txtfiles
 
2Bytesprog2 course_2014_c2_records
2Bytesprog2 course_2014_c2_records2Bytesprog2 course_2014_c2_records
2Bytesprog2 course_2014_c2_records
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
 
2 BytesC++ course_2014_c12_ polymorphism
2 BytesC++ course_2014_c12_ polymorphism2 BytesC++ course_2014_c12_ polymorphism
2 BytesC++ course_2014_c12_ polymorphism
 
2 BytesC++ course_2014_c11_ inheritance
2 BytesC++ course_2014_c11_ inheritance2 BytesC++ course_2014_c11_ inheritance
2 BytesC++ course_2014_c11_ inheritance
 
2 BytesC++ course_2014_c10_ separate compilation and namespaces
2 BytesC++ course_2014_c10_ separate compilation and namespaces 2 BytesC++ course_2014_c10_ separate compilation and namespaces
2 BytesC++ course_2014_c10_ separate compilation and namespaces
 
2 BytesC++ course_2014_c8_ strings
2 BytesC++ course_2014_c8_ strings 2 BytesC++ course_2014_c8_ strings
2 BytesC++ course_2014_c8_ strings
 

Último

Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 

Último (20)

Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 

Pointers and dynamic memory in C

  • 1. Kinan keshkeh IT Engineering-Damascus University 3rd year Summer course- 2014 2 bytes team
  • 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;
  • 7. Pointers •int* findOtherPointer(int* p); Pointers can be Returned type
  • 8. Pointers 42 42 v1 = 0; p1 = &v1; *p1 = 42; cout << v1 << endl; cout << *p1 << endl;
  • 9. Pointers 42 42 v1 = 0; p1 = &v1; *p1 = 42; cout << v1 << endl; cout << *p1 << endl;
  • 10. Pointers •See the difference
  • 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 )
  • 27. ARRAYS & POINTERS Output :
  • 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 !!!
  • 35. Dynamic array delete a [] ; « Illegal »
  • 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
  • 46. ARRAYS & classes struct Record { int number; char grade; }; Record *p; p = new Record; p->number = 2001; p->grade = ’A’; // <===========> (*p).grade=‘A’ •The operator “ -> “ :
  • 47. ARRAYS & classes class Sample { public: ... void showStuff( ) const; ... private: int stuff; ... }; •THE this POINTER :
  • 48. ARRAYS & classes void Sample::showStuff( ) const { cout << stuff; } //Not good style, but this illustrates the this pointer void Sample::showStuff( ) { cout << this->stuff; } •THE this POINTER :
  • 49. ARRAYS & classes •THE this POINTER : It is useful when you have a collision with another variable at the same namespace !
  • 50. ARRAYS & classes #include <iostream> #include <cstdlib> using namespace std; class DayOfYear{ public: void input( ); void output( ); void set(int month, int day); void set(int month); int getMonthNumber( ); int getDay( ); private: int month; int day; }; void DayOfYear::set(int month, int day) { if ((month >= 1) && (month <= 12)) this->month = month; else{ cerr << "Illegal month value! Program aborted.n"; exit(1); } if ((day >= 1) && (day <= 31)) this->day = day; else{ cout << "Illegal day value! Program aborted.n"; exit(1); } }
  • 58. Homework • سؤال عممي : ي ا رد بناء سمسمة خطية لمسيا ا رت لشركة ما حيث لكل سيارة )رقم السيارة – تاريخ التسجيل ( • المطموب: 1 - كتابة اج ا رئية Insert لبناء السمسة بحيث يضاف المعطى حسب الرقم بالمكان الصحيح 2 - كتابة اج ا رئية Print لطباعة بيانات السمسمة 3 - كتابة تابع يقوم بالبحث عن عنصر حسب الرقم 4 -كتابة تابع يقوم بمقارنة تاريخين 5 - طباعة السيا ا رت المسجمة قبل تاريخ معين بالاستعانة بالتابع السابق
  • 59. That’s for today That’s for today guys !!
  • 60. That’s for today Go and Play ! Bye Bye !
  • 61. 2 bytes team Group : group link Mobile phone- Kinan : 0994385748 Facebook account : kinan’s account 2 bytes team