SlideShare ist ein Scribd-Unternehmen logo
1 von 10
Data Types and Pointer Arithmetic's
Why we need Memory at Run Time?
Content to be covered
• Example program to understand Data types
• How data types works for Pointers
• Pointer Arithmetic's
• Pointer to an array
• Need of Dynamic Memory Allocation?
Twists & Turns
What output you expect?
• 2 2
• 4 4
• 1 1
?What you think is it right?
No, correct output is:
• 2 2
• 4 2
• 1 2
• The Reason: Because pointer is made to store addresses
so only 2 bytes are required because address of anything
(int, float, char) is integer number only.
• Then question should come in your mind what is the
significance of data types with respect to pointers?
Pointer Arithmetic's
• Consider the following array:
• int a[5] then compiler will provide following memory addresses.
• a
• Now if a=1000 then what is a+1=? Should it be 1001?
• No!!!, a+1 should be 1002, you might be surprised how it is 1002 it should be 1001
but consider we are talking about addresses so next address is 1002 similarly 1004,
1006 etc. are the addresses of next locations.
1000, 1001 1002, 1003 1004, 1005 1006, 1007 1008, 1009
Pointer Arithmetic's Cont.
• If previous array would have belonging to floats then
a=1000 and a+1 would have been 1004.
• Similarly for other data types.
Need of Dynamic Memory?
• int a[n], just imagine can you have such an array declaration ?
• So what you think YES or NO ?
• Answer is NO, because compiler cannot compute (2*n) bytes of memory to allocate.
• Even if you take a scanf(%d”,&n) statement before this statement still it is not possible because
scanf will take input at run time and compiler needs to allocate memory at compile time.
• So to solve this problem we had to declare int a[1000] or any big number and then we take n
inputs.
• But this way we were wasting memory, so to reduce this wastage of memory we need to learn
Dynamic Memory Allocation.
Pointer Size
Imp. Note: Every pointer variable always consumes 2 bytes in memory
no matter whatever is its data type.
Before learning why it happens let’s prove this statement with the help of a
small program. Consider the following C program.
/*Program for finding out size of pointers*/
#include<stdioh>
#include<conio.h>
void main()
{
int a, *b;
float c, *d;
char e, *f;
printf(“%d%d”,sizeof(a), sizeof(b));
printf(“%d%d”, sizeof(c), sizeof(d));
printf(“%d%d”, sizeof(e), sizeof(f));
getch();
}
You may think that its output may
be 22, 44, 11 but when you will
execute this code on C compiler
surprisingly you will see 22. 42, 12
on output screen, so it proves the
above statement.
Now let’s understand the reason:
Remember pointers are made to
address only and as explained in
introduction section address can
never be a float or character in itself
therefore pointers are never required
to store any float or character value
so it never needs a memory size of
other then 2 bytes. So size of pointer
is always 2 bytes irrespective of its
data type.

Weitere ähnliche Inhalte

Was ist angesagt?

Chapter 05 computer arithmetic
Chapter 05 computer arithmeticChapter 05 computer arithmetic
Chapter 05 computer arithmetic
IIUI
 
Number System
Number SystemNumber System
Number System
Web Designer
 
Matlab HTI summer training course Lecture3
Matlab HTI summer training course Lecture3Matlab HTI summer training course Lecture3
Matlab HTI summer training course Lecture3
Mohamed Awni
 
Csc1401 lecture03 - computer arithmetic - arithmetic and logic unit (alu)
Csc1401   lecture03 - computer arithmetic - arithmetic and logic unit (alu)Csc1401   lecture03 - computer arithmetic - arithmetic and logic unit (alu)
Csc1401 lecture03 - computer arithmetic - arithmetic and logic unit (alu)
IIUM
 
Matlab HTI summer training course_Lecture2
Matlab HTI summer training course_Lecture2Matlab HTI summer training course_Lecture2
Matlab HTI summer training course_Lecture2
Mohamed Awni
 

Was ist angesagt? (20)

Introduction to binary number system
Introduction to binary number systemIntroduction to binary number system
Introduction to binary number system
 
Write a complete C++ program that does the following: 1. The program will c...
Write a complete C++ program that does the following:   1. The program will c...Write a complete C++ program that does the following:   1. The program will c...
Write a complete C++ program that does the following: 1. The program will c...
 
Chapter 05 computer arithmetic
Chapter 05 computer arithmeticChapter 05 computer arithmetic
Chapter 05 computer arithmetic
 
Algoritmos
AlgoritmosAlgoritmos
Algoritmos
 
Abir ppt3
Abir ppt3Abir ppt3
Abir ppt3
 
Number System
Number SystemNumber System
Number System
 
Decimal to Binary Conversion
Decimal to Binary ConversionDecimal to Binary Conversion
Decimal to Binary Conversion
 
20181204i mlse discussions
20181204i mlse discussions20181204i mlse discussions
20181204i mlse discussions
 
Matlab HTI summer training course Lecture3
Matlab HTI summer training course Lecture3Matlab HTI summer training course Lecture3
Matlab HTI summer training course Lecture3
 
Hexadecimal Conversion
Hexadecimal ConversionHexadecimal Conversion
Hexadecimal Conversion
 
Uses Of Calculus is Computer Science
Uses Of Calculus is Computer ScienceUses Of Calculus is Computer Science
Uses Of Calculus is Computer Science
 
Number System
Number SystemNumber System
Number System
 
Applications of Linear Algebra in Computer Sciences
Applications of Linear Algebra in Computer SciencesApplications of Linear Algebra in Computer Sciences
Applications of Linear Algebra in Computer Sciences
 
Octal COnversion
Octal COnversionOctal COnversion
Octal COnversion
 
Csc1401 lecture03 - computer arithmetic - arithmetic and logic unit (alu)
Csc1401   lecture03 - computer arithmetic - arithmetic and logic unit (alu)Csc1401   lecture03 - computer arithmetic - arithmetic and logic unit (alu)
Csc1401 lecture03 - computer arithmetic - arithmetic and logic unit (alu)
 
6nullables in c#
6nullables in c#6nullables in c#
6nullables in c#
 
Matlab HTI summer training course_Lecture2
Matlab HTI summer training course_Lecture2Matlab HTI summer training course_Lecture2
Matlab HTI summer training course_Lecture2
 
Using minitab exec files
Using minitab exec filesUsing minitab exec files
Using minitab exec files
 
Binary to Decimal Conversion
Binary to Decimal ConversionBinary to Decimal Conversion
Binary to Decimal Conversion
 
Assignment 2
Assignment 2Assignment 2
Assignment 2
 

Ähnlich wie Pointers lesson 3 (data types and pointer arithmetics)

Ähnlich wie Pointers lesson 3 (data types and pointer arithmetics) (20)

Lec08-CS110 Computational Engineering
Lec08-CS110 Computational EngineeringLec08-CS110 Computational Engineering
Lec08-CS110 Computational Engineering
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptx
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
Aspdot
AspdotAspdot
Aspdot
 
Data type
Data typeData type
Data type
 
Data oriented design and c++
Data oriented design and c++Data oriented design and c++
Data oriented design and c++
 
Practical deep learning for computer vision
Practical deep learning for computer visionPractical deep learning for computer vision
Practical deep learning for computer vision
 
Begin with c++ Fekra Course #1
Begin with c++ Fekra Course #1Begin with c++ Fekra Course #1
Begin with c++ Fekra Course #1
 
Acm aleppo cpc training second session
Acm aleppo cpc training second sessionAcm aleppo cpc training second session
Acm aleppo cpc training second session
 
Cse115 lecture02overviewofprogramming
Cse115 lecture02overviewofprogrammingCse115 lecture02overviewofprogramming
Cse115 lecture02overviewofprogramming
 
DSA
DSADSA
DSA
 
Pointer
PointerPointer
Pointer
 
Programming Techniques.pptx
Programming Techniques.pptxProgramming Techniques.pptx
Programming Techniques.pptx
 
Arrays.pptx
Arrays.pptxArrays.pptx
Arrays.pptx
 
aspice
aspiceaspice
aspice
 
Introduction to c part -3
Introduction to c   part -3Introduction to c   part -3
Introduction to c part -3
 
Introduction to Parallelization and performance optimization
Introduction to Parallelization and performance optimizationIntroduction to Parallelization and performance optimization
Introduction to Parallelization and performance optimization
 
if, while and for in Python
if, while and for in Pythonif, while and for in Python
if, while and for in Python
 
Automating Tinder w/ Eigenfaces and StanfordNLP
Automating Tinder w/ Eigenfaces and StanfordNLPAutomating Tinder w/ Eigenfaces and StanfordNLP
Automating Tinder w/ Eigenfaces and StanfordNLP
 
Lecture1
Lecture1Lecture1
Lecture1
 

Kürzlich hochgeladen

FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
dharasingh5698
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Kürzlich hochgeladen (20)

NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 

Pointers lesson 3 (data types and pointer arithmetics)

  • 1. Data Types and Pointer Arithmetic's Why we need Memory at Run Time?
  • 2. Content to be covered • Example program to understand Data types • How data types works for Pointers • Pointer Arithmetic's • Pointer to an array • Need of Dynamic Memory Allocation?
  • 4. What output you expect? • 2 2 • 4 4 • 1 1 ?What you think is it right?
  • 5. No, correct output is: • 2 2 • 4 2 • 1 2 • The Reason: Because pointer is made to store addresses so only 2 bytes are required because address of anything (int, float, char) is integer number only. • Then question should come in your mind what is the significance of data types with respect to pointers?
  • 6. Pointer Arithmetic's • Consider the following array: • int a[5] then compiler will provide following memory addresses. • a • Now if a=1000 then what is a+1=? Should it be 1001? • No!!!, a+1 should be 1002, you might be surprised how it is 1002 it should be 1001 but consider we are talking about addresses so next address is 1002 similarly 1004, 1006 etc. are the addresses of next locations. 1000, 1001 1002, 1003 1004, 1005 1006, 1007 1008, 1009
  • 7. Pointer Arithmetic's Cont. • If previous array would have belonging to floats then a=1000 and a+1 would have been 1004. • Similarly for other data types.
  • 8. Need of Dynamic Memory? • int a[n], just imagine can you have such an array declaration ? • So what you think YES or NO ? • Answer is NO, because compiler cannot compute (2*n) bytes of memory to allocate. • Even if you take a scanf(%d”,&n) statement before this statement still it is not possible because scanf will take input at run time and compiler needs to allocate memory at compile time. • So to solve this problem we had to declare int a[1000] or any big number and then we take n inputs. • But this way we were wasting memory, so to reduce this wastage of memory we need to learn Dynamic Memory Allocation.
  • 9. Pointer Size Imp. Note: Every pointer variable always consumes 2 bytes in memory no matter whatever is its data type. Before learning why it happens let’s prove this statement with the help of a small program. Consider the following C program.
  • 10. /*Program for finding out size of pointers*/ #include<stdioh> #include<conio.h> void main() { int a, *b; float c, *d; char e, *f; printf(“%d%d”,sizeof(a), sizeof(b)); printf(“%d%d”, sizeof(c), sizeof(d)); printf(“%d%d”, sizeof(e), sizeof(f)); getch(); } You may think that its output may be 22, 44, 11 but when you will execute this code on C compiler surprisingly you will see 22. 42, 12 on output screen, so it proves the above statement. Now let’s understand the reason: Remember pointers are made to address only and as explained in introduction section address can never be a float or character in itself therefore pointers are never required to store any float or character value so it never needs a memory size of other then 2 bytes. So size of pointer is always 2 bytes irrespective of its data type.