Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

C Programming Summary

Contents

C BASICS ----------------------------------------------------------------------------- 3
1 DATA TYPES --------------------------------------------------------------------------------- 3
1.1 Primitives / Basic Types ----------------------------------------------------------------------- 3
1.2 Casting/Type Conversion --------------------------------------------------------------------- 5
2 OPERATORS --------------------------------------------------------------------------------- 6
2.1 Increment & Decrement Operators ---------------------------------------------------------- 6
2.2 Arithmetic Operators ------------------------------------------------------------------------- 6
2.3 Relational Operators ------------------------------------------------------------------------- 6
2.4 Equality Operators ---------------------------------------------------------------------------- 7
2.5 Logical Operators ------------------------------------------------------------------------------ 7
2.6 Bitwise Operators ----------------------------------------------------------------------------- 7
2.7 Conditional Operators ------------------------------------------------------------------------ 8
2.8 Operators Precedence ------------------------------------------------------------------------ 8
3 SELECTION STATEMENTS -------------------------------------------------------------------- 9
3.1 if Statement ------------------------------------------------------------------------------------ 9
3.2 Switch Statement ---------------------------------------------------------------------------- 10
4 LOOPS ------------------------------------------------------------------------------------- 10
4.1 While Statement ------------------------------------------------------------------------------ 10
4.2 Do While Statement ------------------------------------------------------------------------- 10
4.3 for Statement --------------------------------------------------------------------------------- 11
4.4 Exit from loop -------------------------------------------------------------------------------- 11
5 ARRAYS ----------------------------------------------------------------------------------- 12
5.1 Declaration of Array -------------------------------------------------------------------------- 13
5.2 Initialization of Array ------------------------------------------------------------------------ 13
5.3 One Dimensional Array ---------------------------------------------------------------------- 14
5.4 Two Dimensional Array ---------------------------------------------------------------------- 15
6 FUNCTIONS -------------------------------------------------------------------------------- 16
6.1 Concepts -------------------------------------------------------------------------------------- 16
6.2 Function Declaration ------------------------------------------------------------------------- 17
6.3 Function Definition --------------------------------------------------------------------------- 17
6.4 Function Call ---------------------------------------------------------------------------------- 18
6.5 Memory Sections ----------------------------------------------------------------------------- 21
6.6 Storage Classes ------------------------------------------------------------------------------- 23
7 STRUCT ----------------------------------------------------------------------------------- 24
7.1 Struct Definition ------------------------------------------------------------------------------- 24
7.2 Struct Initialization --------------------------------------------------------------------------- 25
7.3 Important Rules ------------------------------------------------------------------------------- 25
7.4 Bit Field ---------------------------------------------------------------------------------------- 26
7.5 Structure Padding ---------------------------------------------------------------------------- 26
7.6 Memory Alignment --------------------------------------------------------------------------- 28
7.7 Structure Packing ----------------------------------------------------------------------------- 29
8 ENUM ------------------------------------------------------------------------------------ 30
9 UNION ------------------------------------------------------------------------------------ 31
10 PRE-PROCESSOR DIRECTIVES ------------------------------------------------------------- 32
10.1 Macro Definition ----------------------------------------------------------------------------- 32
10.2 Function Like Macro ------------------------------------------------------------------------ 33
10.3 Conditional Compilation ------------------------------------------------------------------- 34
10.4 #Pragma ------------------------------------------------------------------------------------- 34
10.5 Macro VS Functions ------------------------------------------------------------------------- 35
11 POINTERS -------------------------------------------------------------------------------- 35
11.1 Introduction ---------------------------------------------------------------------------------- 35
11.2 Pointer Arithmetic --------------------------------------------------------------------------- 36
11.3 Pointer With Structure ---------------------------------------------------------------------- 38
11.4 Pointer With String -------------------------------------------------------------------------- 38
11.5 Double Pointer ------------------------------------------------------------------------------- 39
11.6 Pointer to Function -------------------------------------------------------------------------- 39
11.7 Pointer With Const--------------------------------------------------------------------------- 39
11.8 Types of Pointer ------------------------------------------------------------------------------ 40

Page |2
C Basics

1 Data Types

- User Define :
• Enum
• Typedef
- Derived :
• Arrays
• Strings
• Unios
• Pointers

- Primitives / Basic Types


• Real Values ( Fraction )
• Integer Values ( Signed – Unsigned )

- We will discuss Primitives/Basic Data Types in this


section and other data types it will have its own
section

1.1 Primitives / Basic Types

- It is recommended that using ( Signed – Unsigned ) to avoid


problems
‫ من جهاز للتانى ميحصلش مشاكل‬Project ‫ يفضل عشان لو نقلت ال‬-

Page |3
➔ int
- Size of int is compiler dependent ( 2 Byte – 4 Byte )
- ( Signed / Unsigned ) ( Long / Short ) int
Positive or Negative ‫ عشان تحدد هتستخدم‬Unsigned ‫ و‬Signed ‫استخدام‬ -
Values
‫ الى هتسخدمه عاوز يبقا‬integer Value ‫ ال‬Size ‫ عشان‬long ‫ و‬Short ‫استخدام‬ -
4 Bytes ( Long ) ‫ او‬2 Bytes ( Short )
‫ على حسب ال‬Compiler Dependent ‫ بيبقوا‬Short ‫ و‬Long :‫و خد بالك‬ -
Byte ‫ الى بتسخدمه هيبقوا كام‬Compiler
‫ بتدعمها عشان تزود ال‬Compilers ‫ فيه بعض ال‬Long long int ‫ممكن تالقى برضه‬ -
Size

➔ char
- For Characters ( a,s,b,$,! )
- ( Singed – Unsigned ) char

➔ float – double
- For Floating Point Numbers ( 4.3 , 1.4 , 1.0 )
- float ( 4 bytes )
- double ( 8 bytes )
- long double ( 10 bytes )

➔ bool
- You should include <stdbool.h> to use bool data type
- For true/false types

Page |4
1.2 Casting/Type Conversion

Technical ‫ و ال‬Quizes ‫ كتيرة فال‬Tricks ‫ من اخطر المواضيع و بيبقا فيها‬-


‫ في الشركات ف خد بالك‬Exams

Implicit Casting Explicit Casting


‫ لما‬Automatic ‫ بيعمله‬Compiler ‫ال‬ - ‫ هو الى بيعمله ب ايده و ساعات بينتج عنه‬User ‫ال‬ -
‫يحتاج‬ ‫ عشان هو بيحول من‬Loss in data
High Data Type → Low Data Type

➔ Implicit Casting
- It happen from Low data Type to High Data Type

int main (void)


{ ‫ لكن الناتج‬float ‫ هيبقا‬Equation ‫المفروض ان ناتج ال‬ -
float a,b,c; ‫ يعنى متخزن فال‬int ‫ و هي من نوع‬s ‫هيتخزن فال‬
int s; ‫ القيمة ال‬s ‫ فبالتالى هيتخزن فال‬int ‫ انه‬Memory
s=a+b+c/100+32/4-3*3.1;
// What is the Type of Result ‫ من الناتج‬integer
} Stored Place ‫ وال‬equation ‫افصل بين ال‬ -

➔ Explicit Casting
int main (void)
{ Convert ‫ انه ي‬Compiler ‫ بتجبر ال‬-
double x=1.1;
int sum = (int)x+1;
// This is Explicit Casting
}

Page |5
2 Operators

2.1 Increment & Decrement Operators

Prefix ( ++x ) Postfix ( ++x)


‫ يجمعها األول‬x ‫ ال‬Address ‫ هيروح عند‬- ‫ األول و يطبعها ثم‬x ‫ لل‬Load ‫ هيروح يعمل‬-
‫ ويطبعها‬Load ‫ثم يعملها‬ ‫يجمعها‬

2.2 Arithmetic Operators

➔ Unary :
- Unary plus : +
- Unary minus : -
Positive or Negative ‫ دول اإلشارات‬-

➔ Binary :
- Addition : + , -
- Multiplication : * / % ( Reminder )
int ‫ غير مع ال‬Compiler ‫ ميقدرش يعمل‬% ( Reminder ) ‫ خلى بالك ان‬-
int % int : ‫ فقط‬numbers
- ..\Testing.c:15:38: error: invalid operands to binary %
(have 'int' and 'float')

2.3 Relational Operators

- < > <= >+ :


• If True : 1
• If False : 0
If Statement ‫ و بنسخدمهم ف‬False ‫ او‬True ‫الناتج بتاعهم بيبقا‬ -

Page |6
2.4 Equality Operators

- == ( Equal to )
- != ( Not Equal to )
If Statement ‫ و بنسخدمهم ف‬False ‫ او‬True ‫ الناتج بتاعهم بيبقا‬-

2.5 Logical Operators

- ! ( Logical not )
- && ( Logical and )
- || ( Logical or )
True ‫ عشان الناتج يبقا‬True ‫ الزم الى بتقارن بينهم يبقوا االتنين‬and ‫ في ال‬-
True ‫ عشان الناتج يبقا‬True ‫ الزم الى بتقارن بينهم أي واحد يبقا ب‬or ‫ في ال‬-

2.6 Bitwise Operators

‫ والزم تبقا فاهمهم كويس عشان مستخدمين كتير فال‬Operators ‫ دول اخطر أنواع ال‬-
Bit Manipulation ‫ خصوصا ال‬Embedded

➔ Shift Left (<<)


- It is like multiply by 2*n (n :how many time you need to shift)
• 29<<1 = 58 : Means 29*2 = 58

➔ Shift Right (>>)


- It is like division by 2*n (n :how many time you need to shift)
• 16<<2 = 8 : Means 16/2 = 8

➔ & (and) | (or) ~ (not) ^ (xor)

Page |7
- Clear Bit
• Variable &=~(1<<Bit_Position)
• Assume Bit_Position = 4 , Variable = 10110111
(00010000) Bit_Position ‫ لحد ال‬1 ‫ لل‬Shit ‫هتعمل‬ •
(11101111) 0 ‫ هيبقا‬1 ‫ للكل فال‬Toggle ‫هتعمل‬ •
( 11101111) & (10110111) = ( 10100111) ‫ مع الناتج‬and ‫هتعمل‬ •
- Sit Bit
• Variable |=(1<<Bit_Position)
Clear ‫الدنيا اتوضحت اكتر كدة بسبب ال‬ •
‫ وخلصت‬OR ‫ ثم‬Bit_Postion ‫ لحد ال‬1 ‫ لل‬Shift ‫هتعمل‬ •
- Toggle Bit
• Variable ^=(1<<Bit_Position)
Bit ‫ لل‬Not ‫ ثم تعمل‬Shift ‫هتعمل‬ •

2.7 Conditional Operators

int main()
{
int Num1=12; ‫ على ال‬Check ‫❖ في المثال دا انت بتعمل‬
int Num2=13;
int Result=0; Condition
Result = (Num1<Num2)?Num1:Num2;
/** ? ‫ هيطبع الى بعد عالمة ال‬True ‫• لو هو‬
* If Num1 > Num2 : False --> Result = Num2;
* If Num2 > Num1 : True --> Result = Num1;
: ‫ هيطبع الى بعد عالمة‬False ‫• لو هو‬
*/
printf("Result = %d\n",Result);
// Result = Num1 (12)
}

2.8 Operators Precedence

Precedence Name – Symbol Associative


Increment ++ ( Postfix ) Left → Right
1
Decrement – ( Postfix ) Left → Right
Unary -,+ Right → Left
2
!,~ Right → Left
Increment ++ ( Prefix ) Right → Left
Decrement – ( Prefix ) Right → Left

3 Multiplication * , / Left → Right

Page |8
Addition + , - Left → Right
4
5 Logical Shit >> << Left → Right

Relational Expressions < > <= >= Left → Right


6
Equality == , != Left → Right
7
8 Bitwise Operators & , | , ~ , ^ Left → Right

Logical Operators && , || Left → Right


9
Conditional Operators () ? __ : __ Right → Left
10
Assignments Operators = , += , /+ , >>= Right → Left
11
Comma Operator , Left → Right
12

3 Selection Statements

3.1 if Statement

Condition ‫ على ال‬Check ‫انت بتعمل‬ -


if (/* condition */)
{ ‫ األول‬Code ‫ هينفذ ال‬: True •
/* code */ Condition ‫ و يشوف ال‬if else ‫ هيخش على ال‬: False •
}
else if (/* condition */) ‫ ينفذ الى جواها‬else ‫ هيخش على ال‬False ‫ هينفذ لو‬True ‫لو‬
{ False ( 0 ) ‫ او‬True ( 1 ) ‫ هيبقا اما‬if ‫ جوا ال‬Condition ‫ال‬ -
/* code */
}
else
{
/* code */
}

int k=7;
if (k) // Here K is not 0 so Condition always true
{
/* COde*/
}

int s=10;
if (s-10) // Here condition will be zero so Condition always false
{

/* Code */
}

Page |9
3.2 Switch Statement

‫ هل هتحققها وال ال ولو‬Cases ‫ و تبدا تشوف ال‬Switch ‫بتعمل‬ •


switch (/*expression*/)
{ default ‫ هتخش فال‬expression ‫ حقتت ال‬case ‫مفيش‬
case /* constant-expression */: ‫ بتاعتك‬case ‫ دي مهمة عشان تنهى ال‬break : ‫خلى بالك‬ •
/* code */ ‫ نفسها و الى تحتها على‬case ‫النها لو مش موجودة فهو هينفذ ال‬
break;
‫ يتنفذوا لو‬case ‫طول و ساعات دى بتبقا مفيدة برضه لو عاوز كذا‬
default: ‫ معين دخل‬expression
break;

}

4 Loops

4.1 While Statement

- Controlling expression testing before


while (/* condition */)
{ loop body executed
/* code */ ‫ األول‬Condition ‫ على ال‬Check ‫ بتعمل‬While ‫ال‬ -
} ‫و تشوفه لو هو‬

‫ هينفذ الكود‬: True •


while (1); /* Infinite Loop */ ‫ مش هينفذ‬: False •

4.2 Do While Statement

- Controlling expression testing after loop


do body executed
{ ‫ الى جوا لمرة‬Code ‫ أوال هي بتنفذ ال‬Do While ‫ ال‬-
/* code */ ‫ هو‬Condition ‫واحدة على األقل و بعد كدة بتشوف ال‬
} ‫ هينفذ الكود‬: True •
while (/* condition */);
‫ مش هينفذ‬: False •

P a g e | 10
4.3 for Statement

for (size_t i = 0; i < count; i++)


{
/* code */
}
for (;;); /* Infinite Loop */

: ‫ أجزاء مهمة‬3 ‫ بتتكون من‬For statement ‫ ال‬-


‫ جوا‬Definition ‫ و ممكن تعمله‬Count ‫ الى هتعمل عليه ال‬Variable ‫• ال‬
for ‫ال‬
‫ يبقا هت‬true(1)‫ هتشتغل عليه و لو هيتحقق‬for ‫ الى ال‬Condition ‫• ال‬
loop ‫ يبقا هتخرج من ال‬false(0) ‫ متحققش‬loop
‫ دا يزيد او‬variable ‫ الى بتخلى ال‬incremental or decremental ‫• ال‬
‫ ول ال‬condition ‫يقل عشان تشوفه هيحقق ال‬

4.4 Exit from loop

4.4.1 the break statement

: ‫ االستخدام‬-
‫ الى احنا فيها‬Loop ‫عشان نكسر ال‬ •
Condition ‫الزم تبقا مصحوبة ب‬ •
‫ الى انت فيها‬Loop ‫ فهى هتخرجك من ال‬Nested loop ‫لو عندك‬ •

int main()
{
int Num=10;
for(int counter=0;counter<Num;counter++)
{
if (counter%2==0) ‫ لو طلع‬condition ‫ و بتشوف ال‬for ‫انت بتخش جوا ال‬ •
break;
if (counter<Num) ‫ و مش هيكملها‬loop ‫ من ال‬break ‫ ف هيحصل‬true(1)
{ ‫خالص هيخرج منها يعنى‬
printf(" Statement One ") loop ‫ فهتكمل باقى ال‬false(0) ‫ طلع‬condition ‫لو ال‬ •
} ‫عادى‬
}

P a g e | 11
4.4.2 the continue statement

: ‫ االستخدام‬-
( ‫ نفسها‬loop ‫ او ال‬condition ‫ الى تحتها سواء تبع ال‬code ‫ لباقى ال‬Skip ‫بتعمل‬ •
‫ تانى‬for ‫ لكله ) و تطلع تنفذ ال‬skip ‫هتعمل‬

int main()
{
int Num=10;
for(int counter=0;counter<Num;counter++)
{
if (counter%2==0)
continue;
if (counter<Num)
{
printf(" Statement ")
}
} ‫ للباقى‬skip ‫ ف هيعمل‬true(1) ‫ اتحقق‬condition ‫لو ال‬ •
}
‫ ينفذ و هكذا‬loop ‫و يطلع لل‬

4.4.3 the goto statement

goto label; ‫استخدامها مش كتير و محدش بيستخدمها يعتبر‬ -


label: /* code */ ‫ ما و تخش جواه‬condition ‫ ولما يحصل‬label ‫الفكرة انك بتعمل‬ -
loop ‫ زي ال‬label ‫ ف هي بتروح تلقائيا على ال‬goto ‫تالقى‬
‫كدة‬

5 Arrays

- Is type of Data Structure that stored collection of elements each


element:
• Have Same Type of Data
• Have position in array “ Index “
• Are stored behind each other
• Stared from index = 0
- Static Configurations can’t change it in runtime

P a g e | 12
- Problem : Doesn’t have Boundary Check
‫ مش‬Arrays ‫ بتاع ال‬Size ‫ مش ضمن ال‬Location ‫• بمعنى انك لو كتبت ف‬
‫ او حاجة تقولك انك اشتغلت غلط ال‬error ‫هتالقى‬

5.1 Declaration of Array

Data_Type Array_Name[Array Size];


Example:
int Array[10];

- So first index = 0 & Last index = 9


- Index from 0 to 9 ( Array_Size-1 )

5.2 Initialization of Array

int Array[10]={1,2,3,4,5,6,7,8,9,10};
int Array[10]={1,2,3,4,5}; // Element from 5 to 9 = zero
int Array[]={1,2,3,4,5}; // Compiler Can know the size by count
elements

P a g e | 13
5.3 One Dimensional Array

int Array[4]={};

- The Normal Array as we see above


‫ بالشكل دا‬Memory ‫ بتتخزن ورا بعض فال‬Elements ‫ ال‬-
4-byte ‫ والتانى‬element ‫ فالفرق بين كل‬int ‫ هو‬Array ‫ ال‬Type ‫ و بما ان‬-
- Important:
• Array_Name is the address of the first element
• Array_Name make you reach to the content of array
‫ ليها‬address ‫ يقدر يوصلك لمحواها بسهولة النه‬Array ‫• اسم ال‬

• Access time to any element in array is constant


‫ هو‬Array[0] ‫ ثابت يعنى زمن وصولك ل‬Array ‫• زمن الوصول الي عنصر فال‬
‫ مش بيعدي‬Address ‫ عشان هو بيروح لل‬Array[3] ‫ بتاع‬Time ‫هو نفس ال‬
element element ‫على‬

P a g e | 14
5.4 Two Dimensional Array

Data_Type Array_Name[Row_Size][Coloun_Size];

- Declaration:
int TwoD_Array[10][10];
- Same Properties as One Dimensional Array
- Looks like Matrix
‫ مبتبقاش شبه ال‬memory ‫ الن ال‬array ‫ في ال‬elements ‫ خلى بالك من ترتيب ال‬-
‫ ورا بعض‬Addresses ‫ ال دى بتبقا‬Matrix

P a g e | 15
6 Functions

6.1 Concepts

• There is Two Types of Functions in C :


• Library Functions
‫ بيوفرهالك‬Compiler ‫▪ و دي ال‬
• User defined Functions
‫ بنفسك‬Create ‫▪ بتعملها‬

• You must differentiate between :


• Function Declaration :
Memory ‫▪ مبيحجزش مساحة فال‬
• Function Definition :
▪ The Code of Function that take Size in Memory and
Function Execute it
‫ والى بياخ مساحة في ال‬Function ‫ الى بتنفذه ال‬Code ‫• ال‬
Memory
• Function Call :
‫ الى جواها‬Code ‫ عشان تنفذ ال‬Function ‫ ال‬Call ‫▪ بت‬
‫ و هنشرحها قدام‬Stack ‫ في ال‬Frame ‫▪ هنا بيبدا يفتح‬

P a g e | 16
6.2 Function Declaration

Return_Type Function_Name ( Type _ Parameter1, Type _ Parameter2)

unsigned int Summing (int Number1 , int Number2);


/**
* @ Return Type : unsigned int
* @ Function Name : Summing
* @ Parameters : Number1 & Number2
*/

6.3 Function Definition

Return_Type Function_Name ( Type _Parameter1, Type _ Parameter2)

/* Your Code */

return Return_Type;

}
unsigned int Summing (int Number1 , int Number2)
{
int Sum=0;
Sum=Number1+Number2;
return Sum;
}

‫ النها مش‬return ‫ ف مش هتكتب‬Void ‫ بتاعها هو‬Return Type ‫ ال‬Function ‫لو ال‬ •


‫هترجع حاجة‬
void ‫ برضه ف هتحط‬Parameters ‫ ميبقاش ليها‬Function ‫ممكن ال‬ •
void Function(void);

‫ عليها‬Validation ‫ تعمل بيه‬Return Type ‫ يبقا ليها‬Function ‫بيبقا األفضل لو ال‬ •


Fail ‫تشوفها اتنفذت وال حصلها‬

P a g e | 17
6.4 Function Call

#include <stdio.h>
unsigned int Summing (int Number1 , int Number2);
/**
* @ Return Type : unsigned int
* @ Function Name : Summing
* @ Arguments : Number1 & Number2
*/
int main ()
{
unsigned int Result =0;
Result = Summing(14,15); // Calling Function
printf("Result = %d\n",Result);
return 0;

}
unsigned int Summing (int Number1 , int Number2)
{
unsigned int Sum=0;
Sum=Number1+Number2;
return Sum;
}

• Notes :
‫ من‬variable ‫ الزم تخزنها في‬main ‫ ف في ال‬Return Type ‫ ليها‬Function ‫لو ال‬ •
Function ‫ بتاع ال‬Type ‫نفس ال‬
‫ ف كدة هيضيع عليك الن ال‬Variable ‫ متخزنش ف‬Function ‫ بتاع ال‬Return ‫لو ال‬ •
‫ بتاعها‬Stack ‫ اول ما بتخلص بتتمسح من ال‬Function
: ‫ و هما كاالتى‬Functions ‫عشان كدة الزن ناخد بالنا من حاجتين في ال‬ •
▪ Call By Value
▪ Call By Reference

• You Should Know :


• Argument :
main ‫ فيه ال‬Function ‫دا الى بتباصيه لل‬ ▪
• Parameter :
Declaration ‫ بتتطلبه في ال‬Function ‫دا الى ال‬ ▪

P a g e | 18
• Call By Value :
• This method copies the actual value of an argument into the
formal parameter of the function. In this case, changes made to
the parameter inside the function have no effect on the
argument.

‫ و بتحطها في ال‬Argument ‫ الى في ال‬Value ‫ لل‬Copy ‫• هنا انت بتعمل‬


‫ و أي تغيير بيحصل في ال‬execute ‫ و بتبدا ت‬Function ‫ بتاع ال‬Parameter
‫ خالص‬Arguments ‫ مش هيأثر على ال‬Parameters
void Function (int Parameter )
{
Parameter ++;
}
‫ تخلص‬Function ‫ و اول ما ال‬increment ‫ هو الى هيحصله‬Parameter ‫هنا ال‬ •
Argument ‫ ومش هياثر في ال‬Stack ‫هيتمسح من ال‬

• Call By Reference :

• This method copies the address of an argument into the formal


parameter. Inside the function, the address is used to access the
actual argument used in the call. This means that changes made
to the parameter affect the argument.

‫ في ال‬Parameter ‫ بتاعك ك‬Argument ‫ بتاع ال‬address ‫• هنا انت بتبعت ال‬


‫ ف هو هيأثر في ال‬Parameter ‫ فبالتالى أي تغيير هيحصل في ال‬Function
‫ هيتأثر و‬Argument ‫ أصال ف ال‬Argument ‫ الى بيشاور على ال‬Address
‫يتغير‬
void Function (int *Parameter )
{ ‫ هيأثر في ال‬Parameter ‫• هنا لما تغير في ال‬
*Parameter ++;
} ‫ وهيتغير‬Argument

P a g e | 19
• How Function call Happen
unsigned int Summing (int Number1 , int Number2);
int main ()
{
unsigned int Result =0;
Result = Summing(14,15); // Calling Function
printf("Result = %d\n",Result);
return 0;

}
unsigned int Summing (int Number1 , int Number2)
{
unsigned int Sum=0;
Sum=Number1+Number2;
return Sum;
}

main ‫ في ال‬Call for Function ‫لما تيجى تعمل‬ •


‫ خاص بيها بمعنى اصح مساحة ليها في‬Frame ‫ ويعملها‬stack in RAM ‫بيروح على ال‬ •
Call ‫ دا مكنش موجود قبل ما تعملها‬Frame ‫ ال‬call ‫ اول ما عملتلها‬stack ‫ال‬
Function ‫ المتعلقة بال‬Data ‫ بيحتوى على كل ال‬Frame ‫ال‬ •
‫ و يحجزلهم أماكنهم و‬Local Variables ‫ بتاعتها و ال‬Parameters ‫بيبدا يشوف ال‬ •
‫ فيهم‬Values ‫ ال‬assign ‫ي‬
Memory ‫كل دا بياخد مساحة من ال‬ •
stack ‫ من ال‬stack frame ‫ كلها بيمسح ال‬operations ‫اول ما ييخلص ال‬ •
area
• Call Function → Stack Frame Create
• Function End → Stack Frame Deleted

P a g e | 20
6.5 Memory Sections

P a g e | 21
- RAM ( Randon Access Memory )
• .data :
Runtime ‫ ( في ال‬RW ) ‫ الى تقدر تعملها‬Data ‫▪ هتالقى فيه كل ال‬
• .bss :
▪ Variables = Zeros in this section
global ‫ بيتحدد على حسب عدد ال‬.bss / .data ‫ بتاع ال‬Size ‫• ال‬
‫ عندك‬definitions ‫ و عدد ال‬variables
‫ انت‬sections ‫ اقرب من كونهم‬segments ‫ يعتبروا‬heap / stack ‫• ال‬
‫ بتاعهم زي ما تحب‬size ‫بحدد ال‬

- ROM ( Read Only Memory )


• .data :
power ‫ عشان ال‬.data in RAM ‫بيتخزن فيه كل ما هو موجود في ال‬ ▪
‫ دي‬data ‫لو فصل متفقدش ال‬
• .rodata :
‫ الى مقتدرش تغيرها حتى اسمها‬constant variables ‫فيها كل ال‬ ▪
read only data
• .text :
assembly ‫ بتاعك بمعمى اصح جواه ال‬code ‫جواه ال‬ ▪
code ‫ الخاصة بال‬instructions
assembly instruction ‫ بتتحول الى‬Function ‫يعنى عندك‬ ▪
‫بتتخزن فيه‬

P a g e | 22
6.6 Storage Classes

Global Local
- Declared outside of any function, typically at - Declared within a block or function, and they
the top of the program file. are only accessible within that block or
function.

- They have a global scope, meaning they can - They have a local scope, meaning they exist
be accessed from anywhere within the and can be accessed only within the block or
program ( Program Scope ) function in which they are declared.
‫ تقجدر‬Function ‫ يقدر يشوفه و أي‬File ‫ كل ال‬Global ‫ ال‬- ( Function – Block Scope )
Operation ‫تعمل عليه‬ ‫ الى متعرف فيه فقط‬Scope ‫ هي ال‬Local ‫ حدود ال‬-

- Global variables have a lifetime equal to the - They are created when the block or function
lifetime of the program. is entered and destroyed when the block or
‫ حتى لو‬File ‫ أي تغيير بيحصل فيهم ف هو متشاف على مستوى ال‬- function exits.
‫ خلصت التغيير هيفضل مأثر فيه‬Function ‫ ولما ال‬Memory ‫ تخلص بيتمسحوا من ال‬Function ‫ اول ما ال‬-
‫ بيتخزنوا من اول و جديد‬Fucntion ‫تيجى تستخدم ال‬

Automatic Static
- Any Local Variable is Automatic By Default - Static variables can be either local to a
- They have a local scope, meaning they are function or global
accessible only within the block or function in - With Global :
which they are declared. ‫ و أي‬extern ‫ كله هيشوفه بس هنا انت مش هتقدر تعمله‬File ‫ ال‬-
File ‫ تانى يقدر يستخدمه ال هو هيبقا متشاف على مستوى ال‬File
( File Scope ) ‫بس‬
- With Local :
‫ ( يعنى أي‬File Scope ) ‫ بس‬File ‫ هيبقا متشاف على مستوى ال‬-
Function ‫تغيير هيحصله هيفضل مأثر فيه مش هيتمسح على لو ال‬
‫خلصت‬
- With Function :
‫ فقط‬File ‫ على مستوى ال‬Function ‫هيخلى ال‬
( File Scope )

Variable Declaration Variable Definition


- extern int var; - int var =7;
‫ فقط‬Variable ‫ انت بتعرف ال‬Declaration ‫ دا كدة اسمه‬- Memory ‫ و بحجزله مساحة في ال‬Var ‫ لل‬Define ‫ بعمل‬-
Memory ‫بس مخدش أي مساحة في ال‬ Declaration ‫وفنفس الوقت انت عملتله‬

P a g e | 23
7 Struct

➢ Structure is a user defined data type which hold or store


Heterogenous or Homogenous data items or elements
‫ متشابهه او مختلفة‬Data ‫بيتخزن فيها‬ •

7.1 Struct Definition

➢ Define struct don’t take size in Memory


‫ النه مش‬Memory ‫ مش هياخدش مكان في ال‬struct ‫ لل‬Define ‫• عشان بس متتلخبطش ال‬
create variable from struct ‫ اال لما ت‬assembly ‫بيخزنه ك‬
size ‫ نفسه هو الى ليه‬Variable ‫• ال‬
‫ مش هيالقى حد استخدمه‬struct ‫ من ال‬object ‫• النك لو مش معرف‬

struct S_Student{
int age; ‫ بس‬struct ‫ لل‬Definition ‫هنا انت عملت‬ •
int Id; Memory ‫مخدش مساحة في ال‬
char Name[10];
}; ‫ ودا الى تخد‬object ‫بعدها انت خدت منه‬ •
struct S_Student Student_One; Memory ‫مساحة في ال‬

struct S_Student {
int age; ‫ كمان خدت منه‬Definition ‫هنا انت عملت‬ •
int Id; ‫ دول هياخدوا مساحة فال‬Two Objects
char Name[10];
Memory
} Student_One , Student_Two;

typedef struct {
int age; ‫ عشان تقدر‬Struct ‫دي افضل طريقة لل‬ •
int Id;
User ‫ انه يبقا ال‬S_Student ‫تستخدم‬
char Name[10];
}S_Student; ‫ بتاعك على طول‬define
S_Student Student_One;

P a g e | 24
7.2 Struct Initialization

➢ Can Initialize like array


struct S_Student Student_One={14,1454,"Muhammed"};

➢ Can Initialize :
• Note that : . is for access member
‫ و ان فيها برضه طريقة‬members or Values ‫▪ هتعرف قدام ان دى مع ال‬
access Pointers ‫ عشان ت‬arrow ‫ بيها اسمها‬access ‫تانية ت‬
printf("Enter Student Two Age : \n");
scanf("%d",&Student_Two.age);
printf("Enter Student Two ID : \n");
scanf("%d",&Student_Two.Id);
printf("Enter Student Two Name : \n");
scanf("%s",&Student_Two.Name);

7.3 Important Rules

struct S_Student {
‫ من نفس نوع ال‬object ‫مينفعش تعمل‬ •
int age;
int Id; ‫ نفسه عشان وقتها ال‬struct ‫ داخل ال‬struct
char Name[10]; compiler ‫ وال‬incomplete ‫ بيبقا‬struct
struct S_Student Student_One;
}; ‫مش هيعرف يتعرف عليه‬

➢ Nested Struct
struct S_Names {
char FirstName[10]; struct ‫تقدر تعمل كدة عادى بس طبعا يبقا ال‬ •
char SecondName[10]; ‫ يبقا األول عشان ال‬nested ‫الى هتعمله‬
};
struct S_Student { error ‫ يقدر يتعرف عليه و ميدكش‬compiler
int age;
int Id;
struct S_Names Student_Name;
};

P a g e | 25
7.4 Bit Field

➢ The Most Dangerous Subject in Embedded System because we


use bit field in all Registers of Microcontroller
➢ Important Notes :
• Bit don’t have address in memory but you have access to bit by pointers
• Byte have address in memory and you have access to each bit in Byte

typedef struct {
char bit_One :1;
Data ‫ هنا هيبقا على حسب نوع ال‬Size ‫ال‬ •
char bit_Two :1; Bits ‫ الى حاجو بيه ال‬Typde
char bit_Three : 1; ‫ عشان من‬1 Byte ‫ كله‬struct ‫ ال‬Size ‫يعنى‬ •
char Reserved : 4;
‫ واخد قد ايه‬field ‫ و انت محدد كل‬char ‫نوع‬
}Register; ‫ دا‬Byte ‫من ال‬

‫ بيتكتب ازاى‬Bit Field ‫• دا شكل ال‬


‫ و خصوصا ال‬Technical Exams ‫ و ال‬Interviews ‫• طبعا بيبقا عليه كم اسألة كتير فال‬
‫ بيبقا كام و هكذا‬struct ‫ بتاع ال‬Size

7.5 Structure Padding

➢ Each Data member or Variable have its Natural Size Boundary


Address
➢ The Compiler Need to execute code more efficiently and in less
execution time
4 ‫ ليه ال‬int ‫ وال‬1 Byte ‫ ليه‬Char ‫ الخاص بيه يعنى ال‬Natural Size ‫ ليه ال‬Variable ‫كل‬ •
‫ و هكذا بالنسبة للباقي‬Byte
‫ في اسرع وقت ممكن و كمان كفائتة كبيرة‬code ‫ بيحاول انه يخرج ال‬compiler ‫ال‬ •
➢ Addressability:
• Processor ability to access Memory
1. 1 Byte - 1 Byte
2. 2 Byte - 2 Byte ( Half Addressability → Half Word )
3. 4 Byte - 4 Byte ( Four Addressability → Word )
• The Compiler Change the best way to access memory more efficient and in
less execution time
‫ بيشوف احسن طريقة و اسرعهم فالتنفيذ‬Compiler ‫• ال‬

P a g e | 26
➢ Boundary:
• Processor access Memory in Specific addresses
) Address ‫ ( حدود ال‬Memory ‫ ال‬access ‫ هيبدا ي‬processor ‫ ال‬address ‫عند انهى‬ •
➢ Example :

#include <stdio.h>
void Calc_Memory(char *Pointer, int _Size);
typedef struct
{
char Variable_One;
int Variable_Two;
char Variable_Three;
short int Variable_Four;
} S_Cheack_Boundry;

S_Cheack_Boundry Test;
int main(void)
{
int Struct_TotalSize = 0;
Test.Variable_One = 0XAA;
Test.Variable_Two = 0XFFFFFFFF;
Test.Variable_Three = 0X55;
Test.Variable_Four = 0xBBBB;
Struct_TotalSize = sizeof(S_Cheack_Boundry);
printf("-----------------------------------------------------------------\n");
printf("Size of Struct = %d\n", Struct_TotalSize);
printf("-----------------------------------------------------------------\n");
Calc_Memory(&Test, Struct_TotalSize);
return 0;
}

void Calc_Memory(char *Pointer, int _Size)


{
int counter = 0;
for (counter = 0; counter < _Size; counter++)
{
printf("%p\t\t\t%x\n", Pointer, (unsigned char)*Pointer);
Pointer++;
}
}

P a g e | 27
( Arm Architecture )‫ ؟ ( مع افتراض اننا شغالين على‬12 ‫ طلع ب‬Size ‫• ليه ال‬
‫ عرف ان ال‬char ‫ لقاه من نوع‬Variable ‫ بص على اول‬Processor ‫ال‬ •
‫ فبالتالى هيستعمل‬1 Byte ‫ بتاعه ب‬Natural Boundary Address
1 ‫ في‬Value ‫ و هيخزن ال‬instruction called ( strb → Store Byte )
Byte
Natural ‫ هيعرف ان ال‬int ‫ الى بعديه هيالقيه‬Variable ‫هيبص على ال‬ •
‫ فبالتالى هيالقى ان اسرع طريقة هو انه يستخدم‬4 Byte ‫ ليه هو‬Boundary Address
‫ ف هيعمل‬instruction called ( str → Store Word [ 4 Byte ] )
‫ و يببدا ي‬char ‫ الى استخدمها ال‬1 Byte ‫ الى بعد ال‬3 Byte ‫ على ال‬padding
4 Byte ‫ ال‬access
‫ فيعرف ان االحسن انه ي‬char ‫ الى بعديه يالقيه‬Variable ‫هيجى يبص على ال‬ •
1 Byte ‫ ب‬Memory ‫ ال‬access
instruction Called ‫ ف احسن حاجة انه يستخدم‬short ‫يجى على الى بعديه يالقيه‬ •
‫ ل‬Padding ‫( فهيعمل‬strh → store half [ Half word – 2 Byte ] )
Memory ‫ ال‬access ‫ يبدا من هنا ي‬char ‫ بعد ال‬Byte

7.6 Memory Alignment

➢ Compiler Generate instructions for more efficiency (strb -strh -str)


Memory ‫ بيه ال‬access ‫ انه ي‬Instruction ‫ انه يختار افضل‬Alignment ‫• ال‬
‫ عشان دى افضل طريقة هو شايفها‬Padding ‫ هو الى بيعمل ال‬Alignment ‫• ال‬
‫ بيها هي‬Align ‫ و يشوف افضل طريقة انه يعمل‬Variable ‫ بيبص على ال‬Compiler ‫• ال‬
‫ ليها‬Padding ‫ من عندها ف هيعمل‬Align ‫ مش كويسة انه يبدا ي‬Bits ‫ايه و لو لقى ان فيه‬
Execution ‫ المناسب فال‬Address ‫ من ال‬Align ‫ و يبدا ي‬zeros ‫ب‬

P a g e | 28
7.7 Structure Packing

➢ Disable the Alignment


▪ More Execution time
▪ Less Performance
▪ Size of Code Increase ( Instructions Increase )
▪ Don’t waste Memory
Execution ‫ على مفيش بس هيزود ال‬Bits ‫ و ميضعش‬Memory ‫• هنا بيحاول انه يوفر ال‬
Performance ‫ ويقلل ال‬time
‫ واحد و دا هيزود ال‬instruction ‫ هتتعامل معاها ب‬Bit ‫ هتويد النك كل‬Instructions ‫• ال‬
Program ‫ لل‬code size

#pragma pack(1)
typedef struct
{
char Variable_One;
int Variable_Two;
char Variable_Three;
short int Variable_Four;
} S_Cheack_Boundry;

typedef struct S_Cheack_Boundry


{
char Variable_One;
int Variable_Two;
char Variable_Three;
short int Variable_Four;
} __attribute__((packed));

#pragma (pack,1)
typedef struct
{
char Variable_One;
int Variable_Two;
char Variable_Three;
short int Variable_Four;
} S_Cheack_Boundry;
#pragma (pop)

P a g e | 29
8 Enum

➢ Define Constant Integer Variable in way more Readable


➢ Size of enum is 4 (int) not the size of all elements

enum Days {
Saturday, ‫ ليه‬default Value ‫ ألول واحد ف هتبقا ال‬Define ‫• لو انت معملتش‬
Sunday,
Monday,
Zero =
Tuesday, Increment by one ‫• الباقى بيحصله‬
Wednesday,
Thursday,
Friday
};

enum Days {
Saturday = 1,
‫• خلى ببالك برضه من المثال دا‬
Sunday, // Sunday = 2
Monday, // Monday = 3
Tuesday=5,
Wednesday, // Wednesday =6
Thursday, // Wednesday =7
Friday // Friday =8
};

enum Days {
Saturday=0,
‫ ل‬Increment ‫• هنا لما يجي يعمل‬
Sunday=1, ‫ بس‬Zero = ‫ هيالقيه‬Tuesday
Monday=-1,
Tuesday, // Should be Zero but it = 2 Saturday = 0 ‫هو عنده‬
Wednesday, // = 3
Thursday, // =4 ‫• فهيشوف اقرب قيمة محدش خدها و‬
};
Friday 2 ‫يحطها الى هي‬

P a g e | 30
9 Union

➢ Is special data type supports different overlapped data types


Shared Memory ‫ مستخدمه و بيعتمد على ال‬Memory ‫• بيجيب اكبر‬
‫ كله‬enum ‫ ال‬size ‫ عنده ويبقا دا‬Type ‫• هو بيشوف مين اكبر‬
typedef union {
char Byte;
‫ فقط‬double ‫ او‬int ‫ او‬char ‫• هنا انت تقدر تستخم اما ال‬
int FourByte; double ‫ الى هو ال‬type ‫ اكبر‬size ‫ كله هيبقا‬size ‫• ال‬
double EightByte;
}Test_Union; double ‫ وجيت بعد كدة استخدمت ال‬char ‫• لو كتبت في ال‬
char ‫ على قيمة ال‬overlapped ‫فانت هنا هتعمل‬

Shared Memroy ‫• هي دي ال‬


• Double overwrite on int & char
• Int overwrite on char
• If in double Value and you use char the
first bit will equal the value of char and so
on for other
‫ مينفعش تستخدم اتنين‬at same time ‫• هو بيشيل نوع واحد‬
‫في نفس الوقت‬

P a g e | 31
10 Pre-Processor Directives

➢ Any thing start with # is called Preprocessor directive


➢ The Process before compilation
➢ The Out put still Source code but without any #
Compile ‫ هو بيشيلها و يحط المكافئ فيها و بعد كدة تخش يحصلها‬# ‫• أي حاجة فالكود بتبدا ب‬
➢ The Pre-Processor Directives are affect Compile Time
‫ المهم ان لما اجى‬Compile ‫• و حاجة زي كدة مش مهمة النى مش فارق معايا يقعد قد ايه يعمل‬
Compile ‫ بتاعى يبقا سريع ف ياخد الوقت الى يحبه فال‬Target ‫ على ال‬Run ‫اعمل‬

Compilers Interpreters
• Compiler reads the entire source • Line By Line
code of the program and converts it Execute ‫ ثم‬Compile ‫بيمشى سطر سطر بيعمل‬ •
into binary code. ‫ويخش على السطر الى بعده وهكذا‬
• Don’t need Operating System • Need Operating System
• Program Speed is Fast • Program Speed Is Slow
‫ و طلع ال‬Compile ‫ كله و عمله‬File ‫• النه مسك ال‬ ‫ ثم‬Compile ‫بطئ عشان بيعدى على سطر سطر يعمل‬ •
‫ بسرعة‬run ‫ النهائي يقطر يعمله‬executable Execute ‫يعمل‬

10.1 Macro Definition

➢ Macro is Called Text Replacement


‫ يحط المقابل ليها‬# ‫ الي حاجة قبلها‬Replace ‫ بيعمل‬Preprocessor ‫• يعنى في ال‬

#define MACRO 10
#define NUMBER (5)
# \
define \
VALUE \
99
int main(void)
{
int x=0;
int y=0;
int z=0;
x=MACRO;
y=NUMBER;
z=VALUE;
printf("x=%d\ny=%d\nz=%d\n",x,y,z);
return 0;
}

P a g e | 32
Macro ‫دي طرق كتابة ال‬ •
‫ نوع من األمان بس‬brackets ‫يفضل انك تحط‬ •
‫ قبل \ عشان‬space ‫ زي الشكل التالت الزم تحط‬Multi line Macro ‫في ال‬ •
‫ برضه لو ال‬Function like macro ‫ و دي طريقة ف كتابة ال‬error ‫ميقولكش‬
‫ طويلة‬Function
‫ النهم مجرد‬Debug ‫ عشان تقدر تعملهم‬Symbol ‫ ملهاش‬Macros ‫خلى بالك ان ال‬ •
program ‫ في ال‬symbols ‫ مش واخدين‬text replacement

➢ # : Convert Macro to String literal


➢ ## : Concatenate two words
_ ‫• بيجملع جملتين على بعض و بيحط بينهم‬
• Func##name → Func_name

10.2 Function Like Macro

#define SUM(NUM1,NUM2) (NUM1+NUM2)


// Parameters // Expression

int main(void)
{ Expression ‫ يحط ال‬SUM ‫ لل‬Text Replace ‫بيعمل‬ •
int Number1=4; ‫المقابل لها‬
int Number2=12;
int Result =SUM(Number1,Number2); ‫ عشان لو مش الزقة‬SUM ‫الزم ال )( تبقا الزقة في ال‬ •
printf("Reslut = %d",Result); ‫ في ال‬error ‫ بتاعها و يديك‬Replacement ‫هيعتبرها ال‬
return 0;
} Symbol ‫ ومش هتعرف هو فين عشان دول ملهمش‬Code
Memory ‫في ال‬

#define PRINT(...) printf(__VA_ARGS__) ‫( … معناهم انهم هياخد أي حاجة و‬anything else) ‫ال‬ •
PRINT("Hello World \n");
__VA_ARGS__ ‫ ويحطهم في‬Replace ‫يعملهم‬

#define DEBUG_INFO(_DEBUGIP,...) \
if(_DEBUGIP>1) \
printf("@ Function : %s , File : %s , Line : %d ",__func__,__FILE__,__LINE__); \
printf(" >> \t"); \
printf(__VA_ARGS__);

P a g e | 33
10.3 Conditional Compilation


#define TRUE 1 #endif ‫ تنتهى ب‬#if ‫الزم أي‬ •
#define FALSE 0
‫ بس دا بيتم في‬if condition ‫استخدامها مشابه لل‬ •
#if TRUE Preprocessor ‫مرحلة ال‬
// Condition
#endif
//________________________________________

#if TRUE
// Condition
#else
//Condition
#endif
//_______________________________________

#if TRUE
// Condition
#else if FALSE
// Condition
#endif

#define VARIABLE 1
‫ هل اتعرفت‬Macros ‫ على ال‬check ‫هنا بتعمل‬ •
‫وال ال‬
#ifdef VARIABLE ‫ مش‬Macro ‫المثال الى تحت بيوريك ان لو ال‬ •
// Condition
#endif ‫متعرف ف هو هيروح يعرفه و الطريقة دي مستخدمه‬
File ‫ عشان تتأكد ان ال‬Guard File ‫في ال‬
#ifndef VARIABLE2
#define VARIABLE2
‫موجود‬
/**
* code
*/
#endif

10.4 #Pragma

➢ Is a compiler dependent before using it check compiler


documentation
➢ # Pragma startup : execute function before main being executed
➢ # Pragma exit : execute function after main being execute

P a g e | 34
10.5 Macro VS Functions

Macros Functions
• Is Preprocessed • Is Compiled
‫ ف مش هتقدر تعملها‬Compilation ‫بتحصل قبل ال‬ • ‫ عادى الن ليها مكان فال‬Debug ‫تقدر تعملها‬ •
Debug symbol ‫ وليها‬Memory
• Not Type Checking • There is Type Checking
• Faster : Text Replacement • Slower :
‫بيشيل و يحط على طول‬ • ‫ و ال‬call ‫ و بيتعملها‬stack ‫عشان بيتفتحلها‬ •
‫ بيتحذف لما تخلص و هكذا‬stack
• Useful for Small code • Useful for Large code
• Increase Code size • Code Size Remain the Same
‫ ويحط ما يقابلها‬Macro ‫عشان كل مرة بيشل ال‬ • call ‫هي بتتكتب مرة و كل مرة بتحتاجها بيتعملها‬ •

11 Pointers

11.1 Introduction

➢ What is Pointer ?
• Pointer is a Variable in C but he contain Address of Another
Variable ( Point to Another Location )

#include <stdio.h> Num


int main(void)
{
int Num = 10;
int *Ptr = &Num; 0x01 10
printf("Num Adress : %p \n", &Num);
printf("Pointer Value : %p \n",Ptr);
printf("Pointer Address : %p \n",&Ptr);
printf("Num Value :%d\n", Num);
printf("Value that Pointer point to :%d\n", *Ptr);
*Ptr = 7; Ptr
printf("Num Value :%d\n", Num);
return 0;
} 0x05 0x01

P a g e | 35
➢ Notes :
• From the Example above meaning that Pointer Point to Integer Value
• int *Ptr ; → Declaration || int *Ptr =&Num; → Definition
Num ‫ بتاع‬address ‫ لل‬store ‫• مهم عالمة ال & النك بتقوله‬
• * → Issue Read/Write size Pointer Type
Pointer ‫ الى بيشاور عليه ال‬Type ‫ بنوع ال‬Value ‫يعنى يكتب على المكان دا‬ ▪
‫ عليه و يكتب فيها‬Point ‫ بي‬Pointer ‫ هيروح على المكان الى ال‬int ‫ من نوع‬Pointer ‫ال‬ ▪
4 Byte ‫ من‬Data
Size of Pointer Depend on Machine :

Pointer Type ‫ملوش عالقة بال‬ ▪
Machine ‫ بتاع ال‬Address Bus ‫ ف عشان كدة معتمد على ال‬Address ‫هو بيشيل‬ ▪
▪ If Address Bus = 4 Byte → Size of Pointer = 4 Byte
▪ If Address Bus = 1 Byte → Size of Pointer = 1 Byte
• *Ptr =7 →
Num ‫ عشان كدة قيمة ال‬7 ‫ عليه روح اكتبلى فيه‬Point ‫▪ هنا انت بتقوله شايف المكان الى انت بت‬
7 ‫هتساوى‬
Read / Write ‫ النه هنا هيشوف هو هي‬Read / Write ‫ غير في ال‬Pointer Type ‫مش هيفرق معاك ال‬ •
Byte ‫كام‬

11.2 Pointer Arithmetic


int main(void) ‫• العمليات هنا مش نفس المعتاد انك بتزود واحد و هكذا ال‬
{
int Num = 10; ‫ فالعمليات هنا هتزيد‬addresses ‫• انت هنا بتتعامل مع‬
int *Ptr = &Num; ‫ بيشاور عليه‬Pointer ‫ الى ال‬Size ‫بحجم ال‬
Ptr=Ptr+1;
Ptr++;
o Int : increment by 4
Ptr--; o Short : Increment by 2
Ptr=Ptr+3; o Char : Increment by 1
}

Data Type ‫ على نفس ال‬Point ‫ يكون بي‬Pointer ‫• و المهم ان ال‬


Increment by 1 ‫ و جيت عملت‬Address = 0x04 ‫ عند‬Ptr ‫• لو نفترض ان‬
int ‫ النه بيشاور على‬Address 0x08 ‫ و هيبص على ال‬4 ‫ هيزيد ب‬Pointer ‫• هنا ال‬
• ( Ptr+i ) :
o i is incremental number
o this form is equivalent to &Ptr[i] : Same as Array
element Address ‫ هو‬Array ‫ النك عارف ان اسم ال‬Array ‫ مستخدمه في ال‬o
➢ Ptr1 =‫اول‬arr[0]

P a g e | 36
Ptr2 = arr[4]
Ptr1 - Ptr2 = ??
Two Pointers ‫• هيجيب المسافة بين ال‬
Array ‫ لل‬Elements ‫• تقدر تستخدمه فانك تعرف عدد ال‬

➢ Ptr1 > Ptr2


Ptr1 == Ptr2
Same Array ‫ بس يكونوا من‬Two Pointers ‫• تقدر تقارن بين‬

➢ Ptr +1
*Ptr ++
*(Ptr)++
Ptr ‫ مالصقة لل‬++ ‫ نفسه عشان ال‬Address ‫ على ال‬Increment ‫• انت بتعمل‬

➢ (*Ptr)++
Data ‫ لل‬Increment ‫• هتعمل‬

➢ ++Ptr
*++Ptr
*(++Ptr)
‫ األول‬Address ‫ هتحصل على ال‬Increment ‫• ال‬

➢ ++(*Ptr)
‫ األول‬Data ‫ لل‬Increment ‫• هيعمل‬

P a g e | 37
11.3 Pointer With Structure

typedef struct
{
int Data;
char Name[100];
float Marks;
}S_Structure;
void Input_Struct(S_Structure Structure); // Pass By Value
void Input_Struct_P(S_Structure *PtrStructure); // Pass By Reference

By ‫ يبقا‬Function ‫ و تبعه ل‬Struct ‫• دايما ويفضل انك لما يجى شغل على‬
Reference
‫ و‬Stack Memory ‫ كبير في ال‬frame ‫ كبير فكدة هيفتح‬Struct ‫• الن لو حجم ال‬
By Value ‫هيستهلك منها لو كان‬

11.4 Pointer With String

int main(void)
{
char *message="CProgramming";
printf("%s\n",message); // Print : CProgramming
*message ='S';
printf("%s\n",message); // Program Crash
return 0;
}

.rodata in flash ‫ بيتخزن في ال‬const ‫ هو‬String ‫• الزم تبقا عارف ان ال‬


‫ فقط‬read ‫ عليه غير انك ت‬access ‫• ملكش أي‬
• If you run the program it will crash because you don’t have
access to write on flash memory

P a g e | 38
11.5 Double Pointer

• Pointer is point to pointer point to value


Var ‫ ل‬address ‫ بيحتوى على‬Pointer ‫ ل‬address ‫ يحتوى على‬Pointer ‫• هو‬

int Variable =9;


int *SPtr=&Variable;
int **DPtr=&SPtr;

**DPtr *SPtr Varaible

11.6 Pointer to Function

void Print_Statements(void);
void (*P_Print) (void);
int main(void)
{
P_Print = Print_Statements;
P_Print();

}
void Print_Statements(void)
{ Lower ‫ و ان ال‬Modularity ‫ بشكل كبير عشان ال‬interrupt ‫• مستخدمة في ال‬
printf("Statement\n");
Function in Upper Layer Directly ‫ ل‬Call ‫ مينفعش تعمل‬Layer
}

11.7 Pointer With Const

• int const *Ptr = &Num;


const int *Ptr = &Num;
▪ Pointer Point to Const Data
‫ تانى‬address ‫ على أي‬Point ‫ يقدر ي‬Pointer ‫ ال‬const ‫ هي ال‬data ‫ال‬ ▪
Num ‫مش هينفع تغير محتوى ال‬ ▪

P a g e | 39
• int *const Ptr = &Num;
▪ Const Pointer Point to Data
‫ بس ال‬Num ‫ تانى غير لل‬Address ‫ على أي‬Point ‫ ميقدرش ي‬Pointer ‫ال‬ ▪
‫ تقدر تغير محواها‬Num

• int const *const Ptr = &Num;


const int *const Ptr = &Num;
▪ const Pointer point to const data
‫ تانى وال تقدر تغير في محتوى ال‬address ‫ على‬Point ‫ تقدر تخليه ي‬Pointer ‫ال ال‬ ▪
Num

11.8 Types of Pointer

Unknown / Void Pointer

▪ Void *Ptr=Null;
‫ لل‬Explicit casting ‫ بس المهم انك تعمله‬data type ‫▪ تقدر تستخدمه مع أي‬
‫ الى هتستخدمه معاه‬data type

Null Pointer

▪ int *Ptr = Null;


‫ يعنى وال حاجة بس مهم دايما انك لو مش هتستخدم ال‬Null ‫▪ بيشاور على‬
Code ‫ عشان ميعملش مشاكل في ال‬Null ‫ دلوقتى انك تخليه ب‬Pointer

Wild Pointer
▪ int *Ptr;
‫ و دا ممكن يعمل مشاكل كتيرة النه ممكن‬initialize ‫ مش معمله‬Pointer ‫▪ هو‬
‫ او تخليه يساوى‬init ‫ لحاجة انت شغال عليها فالفضل تعمله‬address ‫يتخزن فيه‬
Null

P a g e | 40
‫‪Dangling Pointer‬‬

‫;‪▪ int *Ptr = Null‬‬


‫))‪Ptr =malloc(Sizeof(int‬‬
‫)‪free(Ptr‬‬
‫▪ دا بيبقا فالغلب مع ال ‪Dynamic Memory allocation‬‬
‫▪ بتبقا انت استخدمه و عملتله ‪ free‬بس بعد ال ‪ free‬مبيبقاش عارف يتخزن جواه‬
‫ايه ف ممكن يشاور على أي حاجة فال ‪ code‬يبوظها‬
‫▪ األفضل تساويه ب ‪ Null‬بس ما تخلص استخدامه‬

‫‪P a g e | 41‬‬

You might also like