SlideShare a Scribd company logo
1 of 25
Download to read offline
UNIT III
Modular programming:
Function prototype
Function definition
Function call
Built-in functions
Recursion:
Recursive functions
Pointers:
Pointer increment
Pointer arithmetic
Parameter passing:
Pass by value
Pass by reference
Pointer and arrays
Dynamic memory allocation with malloc/calloc
Topics to be Covered:
12/13/2023 1
BVL_Computer Centre, Madras Institute of Technology
Address in C
• Whenever a variable is defined in C, then we can access the
memory address of that variable using & symbol.
• For example, if we define a variable named num then if we
use &num, it will give the variable num’s address in memory.
12/13/2023 2
BVL_Computer Centre, Madras Institute of Technology
Output:
Pointers in C
• Pointers are variables that store the address of other variables.
• A pointer can be used to store the memory address of other
variables, functions, or even other pointers.
• The use of pointers allows low-level memory access, dynamic
memory allocation, and many other functionality in C.
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 3
Pointer declaration
• Like ordinary variables, pointers also have a data type and
must be declared before they are used to store the address
of any variable.
• Pointers are declared with the help of an asterisk (*)
followed by the pointer's name.
• Syntax:
datatype *pointer_variable_name;
• In the above syntax, type refers to what data
type of the variable the pointer will point
to.
• All the pointers, whether integer, float, character, etc., are
of the same data type, a long hexadecimal number
representing the memory address.
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 4
Pointer declaration – Cont’d
• Pointers can be declared in two ways:
• One way is to attach the asterisk with the name of the pointer
variable during the declaration
• i.e. datatype *pointerVariable
• Example: int *ptr;
• Another way is to attach the asterisk at the end of the data
type of which the pointer variable is to be created
• i.e. datatype* pointerVariable
• Example: int* ptr;
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 5
Pointer declaration – Cont’d
• In the previous example, ptr is a pointer, and its type
will be specifically be referred to as "pointer to int",
because it stores the address of an integer variable.
• The type is important.
• While pointers are all the same size, as they just store
a memory address, we have to know what kind of
thing they are pointing to.
• double * dptr; // a pointer to a double
• char * cptr; // a pointer to a character
• float * fptr; // a pointer to a float
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 6
Pointer declaration – Cont’d
• Recall that we can declare multiple variables on one line
under the same type, like this:
int x, y, z; // three variables of type int
• Since the type of a "pointer-to-int" is (int *), we might ask,
does this create three pointers?
int* a, b, c;
• This is not three pointers. Instead, this is one pointer and
two integers.
• If we want to create multiple pointers on one declaration,
we must repeat the * operator each time:
int * a, * b, * c; // three pointers-to-int
• int * a, b, c; // a is a pointer, b and c are integers.
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 7
Initialize a pointer
• After declaring a pointer, we should initialize it like
standard variables with a variable address.
• In C programming, If pointers are uninitialized and used in
the program, then the results are unpredictable and
potentially disastrous.
• Syntax:
pointer = &variable;
• Example: ptr=#
• Note:
• Pointer values and integer values are entirely different
• We cannot store any address in integer variable.
• To store address of any variable we have to declare one variable
as pointer.
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 8
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 9
Output:
Note: In our program pointer variable ptr
contains the address of integer variable num.
To store address of float, char or any other type
variable, we have to declare a pointer of that
type.
Accessing address of register variable
• In the above program, the code tries to get the address of variable
i into the pointer variable p but as i is declared as a register
variable, the code won’t compile and will display the error ” Error:
address of register variable requested”.
• Only certain types of variables are placed into registers.
• Register variables are not given an initial value by the compiler.
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 10
* value at address Operator
(dereference operator)
• This is the second operator used for pointers.
• It is used to access the value present at some address.
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 11
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 12
Output:
UNIT III
Modular programming:
 Function prototype
 Function definition
 Function call
Built-in functions
Recursion:
 Recursive functions
Pointers:
Pointer increment
Pointer arithmetic
Parameter passing:
 Pass by value
 Pass by reference
Pointer and arrays
Dynamic memory allocation with malloc/calloc
Topics to be Covered:
12/13/2023 13
BVL_Computer Centre, Madras Institute of Technology
Pointer Arithmetic
• We can perform arithmetic operations on the pointers
like addition, subtraction, etc.
• The C language allows following operations to be
performed on pointers.
• Increment/Decrement of a Pointer.
• Addition/Subtraction of Integer to a Pointer.
• Differencing (Subtraction of two Pointer of similar type)
• Comparison of two Pointer of similar type.
12/13/2023 14
BVL_Computer Centre, Madras Institute of Technology
Pointer Increment
• If we increment a pointer by 1, the pointer will start pointing
to the immediate next location.
• This is somewhat different from the general arithmetic since
the value of the pointer will get increased by the size of the
data type to which the pointer is pointing.
• The Rule to increment the pointer is given below:
Next_address= current_address + size_of(data type)
• In 32-bit Machine:
• int variable, it will be incremented by 2 bytes.
• In 64-bit Machine:
• int variable, it will be incremented by 4 bytes.
• Similarly, a pointer to a float will be incremented by
sizeof(float) and pointer to char will be incremented by
sizeof(char) .
12/13/2023 15
BVL_Computer Centre, Madras Institute of Technology
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 16
Pointer Increment
Output:
Pointer decrement
• Decrementing a pointer in C simply means to decrease the
pointer value step by step to point to the previous location.
Previous Address = Current Address – size_of(data type)
• Decrementing a pointer to an int will cause its value to be
decremented by sizeof(int).
• In 32-bit Machine:
• int variable, it will be decremented by 2 bytes.
• In 64-bit Machine:
• int variable, it will be decremented by 4 bytes.
• Similarly, a pointer to a float will be decremented by
sizeof(float) and pointer to char will be decremented by
sizeof(char) .
12/13/2023 17
BVL_Computer Centre, Madras Institute of Technology
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 18
Pointer decrement
Output:
Addition/Subtraction of Integer to a Pointer
• C allows integers to be added to or subtracted from pointers.
• When an integer i is added to the pointer, then the new value will
be,
(current address in pointer) + i * sizeof(data_type)
• Example:
• int *ptr, n=20; ptr=&n; ptr=ptr+3
• This code will increment the address in ptr by 3*sizeof(int).
• If the sizeof(int) is 4, then the pointer will increment by 12.
• n=20 and ptr=1000 after ptr=ptr+3 value of ptr becomes 1012.
• Likewise when an integer i is subtracted from a pointer, the the
new value will be,
(current address in pointer) – i * sizeof(data_type)
• Example:
• int *ptr, n=20; ptr=&n; ptr=ptr-3
• This code will decrement the address in ptr by 3*sizeof(int).
• If the sizeof(int) is 4, then the pointer will decrement by 12.
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 19
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 20
Addition/Subtraction of Integer to a Pointer
Output:
Subtracting of two pointers of similar type
(Differencing)
• Differencing is the subtraction of two pointers.
• Subtraction of two pointers is only possible, if they have
same data type.
• When two pointers of same data type is subtracted , their
value is subtracted then result will be divided by the size of
data type.
(address in pointer1 - address in pointer2) / sizeof(data_type)
• If 2 pointers points to 2 different array index of same array
then the subtraction of two pointers gives the number of
elements between the two pointers.
• Example:
int a[5] = {10,20,30,40,50};
int *p, *q;
p=&a[0]; q=&a[3];
printf(“%d”, q-p);
The output of q-p will be (1012-1000)/sizeof(int) i.e 12/4=3.
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 21
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 22
Differencing
Output:
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 23
Differencing
Output:
Comparison of two pointers of similar
type
• Comparison of two pointers is only possible, if they have
same data type.
• Two pointers can be compared by the operators >, ==,
<= , >=.
• It returns TRUE if condition valid, otherwise return
FALSE.
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 24
12/13/2023 BVL_Computer Centre, Madras Institute of Technology 25
Comparison of two pointers of similar type
Output:

More Related Content

Similar to U3_4_PointerArithmetic.pdf people will use the above preaentationa dn

FYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptxFYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptxsangeeta borde
 
0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdfssusere19c741
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Jayanshu Gundaniya
 
Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++Gamindu Udayanga
 
C Programming : Pointers and Strings
C Programming : Pointers and StringsC Programming : Pointers and Strings
C Programming : Pointers and StringsSelvaraj Seerangan
 
Pointers in C PPT | Poinetrs | Pointers Concept
Pointers in C PPT | Poinetrs | Pointers ConceptPointers in C PPT | Poinetrs | Pointers Concept
Pointers in C PPT | Poinetrs | Pointers Conceptsankalpkumarsahoo174
 
Pointers and Array, pointer and String.pptx
Pointers and Array, pointer and String.pptxPointers and Array, pointer and String.pptx
Pointers and Array, pointer and String.pptxAnanthi Palanisamy
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfTamiratDejene1
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxRamakrishna Reddy Bijjam
 

Similar to U3_4_PointerArithmetic.pdf people will use the above preaentationa dn (20)

FYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptxFYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptx
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
pointers (1).ppt
pointers (1).pptpointers (1).ppt
pointers (1).ppt
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
 
Pointers
PointersPointers
Pointers
 
Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
C Programming : Pointers and Strings
C Programming : Pointers and StringsC Programming : Pointers and Strings
C Programming : Pointers and Strings
 
Pointers
PointersPointers
Pointers
 
Pointers in C PPT | Poinetrs | Pointers Concept
Pointers in C PPT | Poinetrs | Pointers ConceptPointers in C PPT | Poinetrs | Pointers Concept
Pointers in C PPT | Poinetrs | Pointers Concept
 
Pointer.pptx
Pointer.pptxPointer.pptx
Pointer.pptx
 
Pointers and Array, pointer and String.pptx
Pointers and Array, pointer and String.pptxPointers and Array, pointer and String.pptx
Pointers and Array, pointer and String.pptx
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdf
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
 
C pointer
C pointerC pointer
C pointer
 
Chapter 2.datatypes and operators
Chapter 2.datatypes and operatorsChapter 2.datatypes and operators
Chapter 2.datatypes and operators
 
Pointer
PointerPointer
Pointer
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 

Recently uploaded

HiFi Call Girl Service Delhi Phone ☞ 9899900591 ☜ Escorts Service at along wi...
HiFi Call Girl Service Delhi Phone ☞ 9899900591 ☜ Escorts Service at along wi...HiFi Call Girl Service Delhi Phone ☞ 9899900591 ☜ Escorts Service at along wi...
HiFi Call Girl Service Delhi Phone ☞ 9899900591 ☜ Escorts Service at along wi...poojakaurpk09
 
8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Available
8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Available8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Available
8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Availabledollysharma2066
 
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...instagramfab782445
 
Hire 💕 8617697112 Meerut Call Girls Service Call Girls Agency
Hire 💕 8617697112 Meerut Call Girls Service Call Girls AgencyHire 💕 8617697112 Meerut Call Girls Service Call Girls Agency
Hire 💕 8617697112 Meerut Call Girls Service Call Girls AgencyNitya salvi
 
💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...
💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...
💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...sonalitrivedi431
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...Pooja Nehwal
 
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...amitlee9823
 
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...kumaririma588
 
Booking open Available Pune Call Girls Kirkatwadi 6297143586 Call Hot Indian...
Booking open Available Pune Call Girls Kirkatwadi  6297143586 Call Hot Indian...Booking open Available Pune Call Girls Kirkatwadi  6297143586 Call Hot Indian...
Booking open Available Pune Call Girls Kirkatwadi 6297143586 Call Hot Indian...Call Girls in Nagpur High Profile
 
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Call Girls in Nagpur High Profile
 
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...Delhi Call girls
 
Peaches App development presentation deck
Peaches App development presentation deckPeaches App development presentation deck
Peaches App development presentation decktbatkhuu1
 
Case Study of Hotel Taj Vivanta, Pune
Case Study of Hotel Taj Vivanta, PuneCase Study of Hotel Taj Vivanta, Pune
Case Study of Hotel Taj Vivanta, PuneLukeKholes
 
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...amitlee9823
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Th...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Th...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Th...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Th...Pooja Nehwal
 
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts ServiceVVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Servicearoranaina404
 
Sector 105, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 105, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 105, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 105, Noida Call girls :8448380779 Model Escorts | 100% verifiedDelhi Call girls
 
Design Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptxDesign Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptxTusharBahuguna2
 
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...amitlee9823
 

Recently uploaded (20)

HiFi Call Girl Service Delhi Phone ☞ 9899900591 ☜ Escorts Service at along wi...
HiFi Call Girl Service Delhi Phone ☞ 9899900591 ☜ Escorts Service at along wi...HiFi Call Girl Service Delhi Phone ☞ 9899900591 ☜ Escorts Service at along wi...
HiFi Call Girl Service Delhi Phone ☞ 9899900591 ☜ Escorts Service at along wi...
 
8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Available
8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Available8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Available
8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Available
 
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
 
Hire 💕 8617697112 Meerut Call Girls Service Call Girls Agency
Hire 💕 8617697112 Meerut Call Girls Service Call Girls AgencyHire 💕 8617697112 Meerut Call Girls Service Call Girls Agency
Hire 💕 8617697112 Meerut Call Girls Service Call Girls Agency
 
💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...
💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...
💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
 
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
 
Booking open Available Pune Call Girls Kirkatwadi 6297143586 Call Hot Indian...
Booking open Available Pune Call Girls Kirkatwadi  6297143586 Call Hot Indian...Booking open Available Pune Call Girls Kirkatwadi  6297143586 Call Hot Indian...
Booking open Available Pune Call Girls Kirkatwadi 6297143586 Call Hot Indian...
 
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
 
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
 
Peaches App development presentation deck
Peaches App development presentation deckPeaches App development presentation deck
Peaches App development presentation deck
 
Case Study of Hotel Taj Vivanta, Pune
Case Study of Hotel Taj Vivanta, PuneCase Study of Hotel Taj Vivanta, Pune
Case Study of Hotel Taj Vivanta, Pune
 
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Th...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Th...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Th...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Th...
 
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts ServiceVVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
 
Sector 105, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 105, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 105, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 105, Noida Call girls :8448380779 Model Escorts | 100% verified
 
Design Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptxDesign Inspiration for College by Slidesgo.pptx
Design Inspiration for College by Slidesgo.pptx
 
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
Whitefield Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Ba...
 

U3_4_PointerArithmetic.pdf people will use the above preaentationa dn

  • 1. UNIT III Modular programming: Function prototype Function definition Function call Built-in functions Recursion: Recursive functions Pointers: Pointer increment Pointer arithmetic Parameter passing: Pass by value Pass by reference Pointer and arrays Dynamic memory allocation with malloc/calloc Topics to be Covered: 12/13/2023 1 BVL_Computer Centre, Madras Institute of Technology
  • 2. Address in C • Whenever a variable is defined in C, then we can access the memory address of that variable using & symbol. • For example, if we define a variable named num then if we use &num, it will give the variable num’s address in memory. 12/13/2023 2 BVL_Computer Centre, Madras Institute of Technology Output:
  • 3. Pointers in C • Pointers are variables that store the address of other variables. • A pointer can be used to store the memory address of other variables, functions, or even other pointers. • The use of pointers allows low-level memory access, dynamic memory allocation, and many other functionality in C. 12/13/2023 BVL_Computer Centre, Madras Institute of Technology 3
  • 4. Pointer declaration • Like ordinary variables, pointers also have a data type and must be declared before they are used to store the address of any variable. • Pointers are declared with the help of an asterisk (*) followed by the pointer's name. • Syntax: datatype *pointer_variable_name; • In the above syntax, type refers to what data type of the variable the pointer will point to. • All the pointers, whether integer, float, character, etc., are of the same data type, a long hexadecimal number representing the memory address. 12/13/2023 BVL_Computer Centre, Madras Institute of Technology 4
  • 5. Pointer declaration – Cont’d • Pointers can be declared in two ways: • One way is to attach the asterisk with the name of the pointer variable during the declaration • i.e. datatype *pointerVariable • Example: int *ptr; • Another way is to attach the asterisk at the end of the data type of which the pointer variable is to be created • i.e. datatype* pointerVariable • Example: int* ptr; 12/13/2023 BVL_Computer Centre, Madras Institute of Technology 5
  • 6. Pointer declaration – Cont’d • In the previous example, ptr is a pointer, and its type will be specifically be referred to as "pointer to int", because it stores the address of an integer variable. • The type is important. • While pointers are all the same size, as they just store a memory address, we have to know what kind of thing they are pointing to. • double * dptr; // a pointer to a double • char * cptr; // a pointer to a character • float * fptr; // a pointer to a float 12/13/2023 BVL_Computer Centre, Madras Institute of Technology 6
  • 7. Pointer declaration – Cont’d • Recall that we can declare multiple variables on one line under the same type, like this: int x, y, z; // three variables of type int • Since the type of a "pointer-to-int" is (int *), we might ask, does this create three pointers? int* a, b, c; • This is not three pointers. Instead, this is one pointer and two integers. • If we want to create multiple pointers on one declaration, we must repeat the * operator each time: int * a, * b, * c; // three pointers-to-int • int * a, b, c; // a is a pointer, b and c are integers. 12/13/2023 BVL_Computer Centre, Madras Institute of Technology 7
  • 8. Initialize a pointer • After declaring a pointer, we should initialize it like standard variables with a variable address. • In C programming, If pointers are uninitialized and used in the program, then the results are unpredictable and potentially disastrous. • Syntax: pointer = &variable; • Example: ptr=&num; • Note: • Pointer values and integer values are entirely different • We cannot store any address in integer variable. • To store address of any variable we have to declare one variable as pointer. 12/13/2023 BVL_Computer Centre, Madras Institute of Technology 8
  • 9. 12/13/2023 BVL_Computer Centre, Madras Institute of Technology 9 Output: Note: In our program pointer variable ptr contains the address of integer variable num. To store address of float, char or any other type variable, we have to declare a pointer of that type.
  • 10. Accessing address of register variable • In the above program, the code tries to get the address of variable i into the pointer variable p but as i is declared as a register variable, the code won’t compile and will display the error ” Error: address of register variable requested”. • Only certain types of variables are placed into registers. • Register variables are not given an initial value by the compiler. 12/13/2023 BVL_Computer Centre, Madras Institute of Technology 10
  • 11. * value at address Operator (dereference operator) • This is the second operator used for pointers. • It is used to access the value present at some address. 12/13/2023 BVL_Computer Centre, Madras Institute of Technology 11
  • 12. 12/13/2023 BVL_Computer Centre, Madras Institute of Technology 12 Output:
  • 13. UNIT III Modular programming:  Function prototype  Function definition  Function call Built-in functions Recursion:  Recursive functions Pointers: Pointer increment Pointer arithmetic Parameter passing:  Pass by value  Pass by reference Pointer and arrays Dynamic memory allocation with malloc/calloc Topics to be Covered: 12/13/2023 13 BVL_Computer Centre, Madras Institute of Technology
  • 14. Pointer Arithmetic • We can perform arithmetic operations on the pointers like addition, subtraction, etc. • The C language allows following operations to be performed on pointers. • Increment/Decrement of a Pointer. • Addition/Subtraction of Integer to a Pointer. • Differencing (Subtraction of two Pointer of similar type) • Comparison of two Pointer of similar type. 12/13/2023 14 BVL_Computer Centre, Madras Institute of Technology
  • 15. Pointer Increment • If we increment a pointer by 1, the pointer will start pointing to the immediate next location. • This is somewhat different from the general arithmetic since the value of the pointer will get increased by the size of the data type to which the pointer is pointing. • The Rule to increment the pointer is given below: Next_address= current_address + size_of(data type) • In 32-bit Machine: • int variable, it will be incremented by 2 bytes. • In 64-bit Machine: • int variable, it will be incremented by 4 bytes. • Similarly, a pointer to a float will be incremented by sizeof(float) and pointer to char will be incremented by sizeof(char) . 12/13/2023 15 BVL_Computer Centre, Madras Institute of Technology
  • 16. 12/13/2023 BVL_Computer Centre, Madras Institute of Technology 16 Pointer Increment Output:
  • 17. Pointer decrement • Decrementing a pointer in C simply means to decrease the pointer value step by step to point to the previous location. Previous Address = Current Address – size_of(data type) • Decrementing a pointer to an int will cause its value to be decremented by sizeof(int). • In 32-bit Machine: • int variable, it will be decremented by 2 bytes. • In 64-bit Machine: • int variable, it will be decremented by 4 bytes. • Similarly, a pointer to a float will be decremented by sizeof(float) and pointer to char will be decremented by sizeof(char) . 12/13/2023 17 BVL_Computer Centre, Madras Institute of Technology
  • 18. 12/13/2023 BVL_Computer Centre, Madras Institute of Technology 18 Pointer decrement Output:
  • 19. Addition/Subtraction of Integer to a Pointer • C allows integers to be added to or subtracted from pointers. • When an integer i is added to the pointer, then the new value will be, (current address in pointer) + i * sizeof(data_type) • Example: • int *ptr, n=20; ptr=&n; ptr=ptr+3 • This code will increment the address in ptr by 3*sizeof(int). • If the sizeof(int) is 4, then the pointer will increment by 12. • n=20 and ptr=1000 after ptr=ptr+3 value of ptr becomes 1012. • Likewise when an integer i is subtracted from a pointer, the the new value will be, (current address in pointer) – i * sizeof(data_type) • Example: • int *ptr, n=20; ptr=&n; ptr=ptr-3 • This code will decrement the address in ptr by 3*sizeof(int). • If the sizeof(int) is 4, then the pointer will decrement by 12. 12/13/2023 BVL_Computer Centre, Madras Institute of Technology 19
  • 20. 12/13/2023 BVL_Computer Centre, Madras Institute of Technology 20 Addition/Subtraction of Integer to a Pointer Output:
  • 21. Subtracting of two pointers of similar type (Differencing) • Differencing is the subtraction of two pointers. • Subtraction of two pointers is only possible, if they have same data type. • When two pointers of same data type is subtracted , their value is subtracted then result will be divided by the size of data type. (address in pointer1 - address in pointer2) / sizeof(data_type) • If 2 pointers points to 2 different array index of same array then the subtraction of two pointers gives the number of elements between the two pointers. • Example: int a[5] = {10,20,30,40,50}; int *p, *q; p=&a[0]; q=&a[3]; printf(“%d”, q-p); The output of q-p will be (1012-1000)/sizeof(int) i.e 12/4=3. 12/13/2023 BVL_Computer Centre, Madras Institute of Technology 21
  • 22. 12/13/2023 BVL_Computer Centre, Madras Institute of Technology 22 Differencing Output:
  • 23. 12/13/2023 BVL_Computer Centre, Madras Institute of Technology 23 Differencing Output:
  • 24. Comparison of two pointers of similar type • Comparison of two pointers is only possible, if they have same data type. • Two pointers can be compared by the operators >, ==, <= , >=. • It returns TRUE if condition valid, otherwise return FALSE. 12/13/2023 BVL_Computer Centre, Madras Institute of Technology 24
  • 25. 12/13/2023 BVL_Computer Centre, Madras Institute of Technology 25 Comparison of two pointers of similar type Output: