Professional Documents
Culture Documents
C Programming Summary
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
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
➔ Implicit Casting
- It happen from Low data Type to High Data Type
➔ Explicit Casting
int main (void)
{ Convert انه يCompiler بتجبر ال-
double x=1.1;
int sum = (int)x+1;
// This is Explicit Casting
}
Page |5
2 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')
Page |6
2.4 Equality Operators
- == ( Equal to )
- != ( Not Equal to )
If Statement و بنسخدمهم فFalse اوTrue الناتج بتاعهم بيبقا-
- ! ( Logical not )
- && ( Logical and )
- || ( Logical or )
True عشان الناتج يبقاTrue الزم الى بتقارن بينهم يبقوا االتنينand في ال-
True عشان الناتج يبقاTrue الزم الى بتقارن بينهم أي واحد يبقا بor في ال-
والزم تبقا فاهمهم كويس عشان مستخدمين كتير فالOperators دول اخطر أنواع ال-
Bit Manipulation خصوصا الEmbedded
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 هتعمل •
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)
}
Page |8
Addition + , - Left → Right
4
5 Logical Shit >> << Left → Right
3 Selection Statements
3.1 if Statement
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
4 Loops
P a g e | 10
4.3 for 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 و يطلع لل
5 Arrays
P a g e | 12
- Problem : Doesn’t have Boundary Check
مشArrays بتاع الSize مش ضمن الLocation • بمعنى انك لو كتبت ف
او حاجة تقولك انك اشتغلت غلط الerror هتالقى
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]={};
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
P a g e | 16
6.2 Function Declaration
/* Your Code */
return Return_Type;
}
unsigned int Summing (int Number1 , int Number2)
{
int Sum=0;
Sum=Number1+Number2;
return Sum;
}
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
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.
• Call By Reference :
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;
}
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 بحدد ال
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 )
P a g e | 23
7 Struct
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 :
• 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);
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
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 من ال
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;
}
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
P a g e | 28
7.7 Structure Packing
#pragma pack(1)
typedef struct
{
char Variable_One;
int Variable_Two;
char Variable_Three;
short int Variable_Four;
} S_Cheack_Boundry;
#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
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
P a g e | 31
10 Pre-Processor Directives
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 يعمل
#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
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
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 )
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 كام
➢
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
}
P a g e | 36
Ptr2 = arr[4]
Ptr1 - Ptr2 = ??
Two Pointers • هيجيب المسافة بين ال
Array للElements • تقدر تستخدمه فانك تعرف عدد ال
➢ 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 هيستهلك منها لو كان
int main(void)
{
char *message="CProgramming";
printf("%s\n",message); // Print : CProgramming
*message ='S';
printf("%s\n",message); // Program Crash
return 0;
}
P a g e | 38
11.5 Double Pointer
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
}
P a g e | 39
• int *const Ptr = &Num;
▪ Const Pointer Point to Data
بس الNum تانى غير للAddress على أيPoint ميقدرش يPointer ال ▪
تقدر تغير محواهاNum
▪ Void *Ptr=Null;
للExplicit casting بس المهم انك تعملهdata type ▪ تقدر تستخدمه مع أي
الى هتستخدمه معاهdata type
Null Pointer
Wild Pointer
▪ int *Ptr;
و دا ممكن يعمل مشاكل كتيرة النه ممكنinitialize مش معملهPointer ▪ هو
او تخليه يساوىinit لحاجة انت شغال عليها فالفضل تعملهaddress يتخزن فيه
Null
P a g e | 40
Dangling Pointer
P a g e | 41