Professional Documents
Culture Documents
Data Structure and Algorithms - محاضرة2
Data Structure and Algorithms - محاضرة2
structure and
algorithms
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
float price;
} book;
التراكيب Structures
Struct_variable.member
#include <iostream>
#include <cstring>
Structures التراكيب
using namespace std;
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main() {
struct Books Book1; // Declare Book1 of type Book
struct Books Book2; // Declare Book2 of type Book
// book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
// book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
return 0;
}
المؤشرات
Pointers
المؤشرات Pointers
المؤشرات هي نوع بيانات -وكما يشير إسمها هي أنها تؤشر أو تشير إلى شيء ما ،وذلك ●
الشيء هو موقع في الذاكرة يحوي قيمة ما ،تكمن أهميتها في أنها تتعامل مع الذاكرة مباشرة
و تعطيك سرعة في برنامجك
تُقسم ذاكرة الحاسوب إلى أجزاء من bytesوكل بايت لديه العنوان الخاص به على سبيل ●
فالمؤشرات إ ًذا هي متغيرات تختزن عناوين متغيرات أخرى في الذاكرة بالـ hexadecimal
النظام السادس عشري
المؤشرات
مثال على طريقة تعريف مؤشر وإسناد عنوان متغير من نوع intإليه:
;int* ptr
;int a = 5
ptr = &a; // &a is the address of a
مثال ●
و حاول أن تستنتج شكل المخرجات، تتبع البرنامج التالي
#include
int main ()
{ int value1 = 5, value2 = 15;
int* p1; int* p2;
p1 = &value1;
p2 = &value2;
*p1 = 10;
*p2 = *p1;
p1 = p2;
*p1 = 20;
cout << "value1==" <<value1<<"\t value2=="<< value2<
تستطيع المؤشرات اإلشارة إلى المتغيرات المفردة كما وتشير إلى عناصر المصفوفة array
لنفترض أن p,aعرفت كما يلي int a[10],*p; :فبهذا يمكن استخدام المؤشر pلإلشارة إلى العنصر
األول من المصفوفة ] a[0كما يلي:
لغة C ++تسمح بإستخدام المؤشر newلحجز الذاكرة الديناميكية والمؤشر deleteلتنظيف الذاكرة يمكن استخدام
المؤشر newلحجز ذاكرة موقع واحد حسب الصيغة التالية:
Delete p-var
مثال :
;)int *x =new int(8
;delete x
cin>>*(x+i);
cout<<" array elements is \n";
for(i=0;i<m;i++)
cout<<*(x+i)<<"\n";
delete [ ] x;
return 0;
}