SlideShare une entreprise Scribd logo
1  sur  14
Data Structure 1
Pointer,
Function, structure, array,
recursive function call
Pointers
Introduction
• Pointers
– Pointers are variables containing memory addresses of a
variable with specific value.
– A variable name directly references a value
– A pointer indirectly reference a value.
– A pointer ‘p’ pointing to a character ‘c’ –
Pointer Variable Declarations and
Initialization
• Pointer variables
– Contain memory address as their value
– Normal variables contain a specific value (direct reference)
– Pointers contain address of a variable that has a specific
value (indirect reference)
– Indirection – referencing a pointer value
• Pointer declarations
– * used with pointer variables: e.g., int *myPtr;
– Declares a pointer to an int (pointer of type int *)
– Multiple pointers require using a * before each variable declaration:
int *myPtr1, *myPtr2;
– Can declare pointers to any data type
– Initialize pointers to 0, NULL, or an address
• 0 or NULL –points to nothing (NULL preferred)
Calling Functions by Reference
• Call by reference with pointer arguments
– Pass address of argument using & operator
– Allows you to change actual location in memory
– Arrays are not passed with & because the array
name is already a pointer.
Pointer Expressions and Pointer
Arithmetic
• Arithmetic operations can be performed on
Pointers
– Increment/decrement pointer (++ or --)
– Add an integer to a pointer( + or += , - or -=)
– Pointers may be subtracted from each other
• 5 element int array on machine with 4 byte int
– vPtr points to first element v[ 0 ]
– at location 3000 (vPtr = 3000)
– vPtr += 2; sets vPtr to 3008
– vPtr points to v[ 2 ] (incremented by 2),
but the machine has 4 byte int, so it points to address 3008
sizeof Operator
• Sizeof
– Returns size of operand in bytes
– For arrays: size of 1 element *number of elements
– sizeof can be used with
• Variable names
• Type name
• Constant values
Relationship Between Pointers and
Arrays
• Arrays and pointers closely related
– Array name like a constant pointer
– Pointers can do array subscripting operations
Pointer-to-pointers
• We can have pointers to – int, char, float, any
structure….
• Declaration of a pointer-to-pointer:
• int
**ipp; where two * indicates two level of
pointers.
• ipp points to ip1 which points to i.
• **ipp is i, or 10.
Pointers to Functions
• Pointer to function
– Contains address of function
– Similar to how array name is address of first element
– Function name is starting address of code that defines
Function
• Function pointers can be
– Passed to functions
– Stored in arrays
– Assigned to other function pointers
Data Structure Books
• Text Books:
• Mark Allen Weiss - Data Structures and Algorithm Analysis in
C, 2nd Edition, Addison-Wesley, 1997
• P.S. Deshpande and O.G. Kakde - C and Data Structures,
Charles Rive Media, 2004
• Reference Books:
• Data structures and problem solving using C++, By Mark Allen
Weiss - Addison-Wesley (2000)

Contenu connexe

Tendances

Pointers in c
Pointers in cPointers in c
Pointers in c
Mohd Arif
 
Chp3(pointers ref)
Chp3(pointers ref)Chp3(pointers ref)
Chp3(pointers ref)
Mohd Effandi
 

Tendances (18)

Double pointer (pointer to pointer)
Double pointer (pointer to pointer)Double pointer (pointer to pointer)
Double pointer (pointer to pointer)
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
Pointer in c++ part1
Pointer in c++ part1Pointer in c++ part1
Pointer in c++ part1
 
Introduction to Python
Introduction to Python  Introduction to Python
Introduction to Python
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
 
Pointers in c v5 12102017 1
Pointers in c v5 12102017 1Pointers in c v5 12102017 1
Pointers in c v5 12102017 1
 
ppt on pointers
ppt on pointersppt on pointers
ppt on pointers
 
C Programming - Refresher - Part II
C Programming - Refresher - Part II C Programming - Refresher - Part II
C Programming - Refresher - Part II
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
Chp3(pointers ref)
Chp3(pointers ref)Chp3(pointers ref)
Chp3(pointers ref)
 
Pointers In C
Pointers In CPointers In C
Pointers In C
 
Advance topics of C language
Advance  topics of C languageAdvance  topics of C language
Advance topics of C language
 
Introduction to pointers and memory management in C
Introduction to pointers and memory management in CIntroduction to pointers and memory management in C
Introduction to pointers and memory management in C
 
C pointer
C pointerC pointer
C pointer
 
8 Pointers
8 Pointers8 Pointers
8 Pointers
 
Python functions
Python functionsPython functions
Python functions
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 

En vedette (6)

Linkers And Loaders
Linkers And LoadersLinkers And Loaders
Linkers And Loaders
 
Program Structure in GNU/Linux (ELF Format)
Program Structure in GNU/Linux (ELF Format)Program Structure in GNU/Linux (ELF Format)
Program Structure in GNU/Linux (ELF Format)
 
The Internals of "Hello World" Program
The Internals of "Hello World" ProgramThe Internals of "Hello World" Program
The Internals of "Hello World" Program
 
PE File Format
PE File FormatPE File Format
PE File Format
 
Memory management
Memory managementMemory management
Memory management
 
Computer memory
Computer memoryComputer memory
Computer memory
 

Similaire à Data structure lecture 1

Similaire à Data structure lecture 1 (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
 
Lecture15.pdf
Lecture15.pdfLecture15.pdf
Lecture15.pdf
 
pointers (1).ppt
pointers (1).pptpointers (1).ppt
pointers (1).ppt
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
Pointers
PointersPointers
Pointers
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
 
Pointer in c++ part2
Pointer in c++ part2Pointer in c++ part2
Pointer in c++ part2
 
20.C++Pointer.pptx
20.C++Pointer.pptx20.C++Pointer.pptx
20.C++Pointer.pptx
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
 
Pointer.pptx
Pointer.pptxPointer.pptx
Pointer.pptx
 
Pointers in c language
Pointers in c languagePointers in c language
Pointers in c language
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
C96e1 session3 c++
C96e1 session3 c++C96e1 session3 c++
C96e1 session3 c++
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Pointer
PointerPointer
Pointer
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdf
 
C Pointers
C PointersC Pointers
C Pointers
 

Plus de Samsil Arefin

Plus de Samsil Arefin (20)

Transmission Control Protocol and User Datagram protocol
Transmission Control Protocol and User Datagram protocolTransmission Control Protocol and User Datagram protocol
Transmission Control Protocol and User Datagram protocol
 
Evolution Phylogenetic
Evolution PhylogeneticEvolution Phylogenetic
Evolution Phylogenetic
 
Evolution Phylogenetic
Evolution PhylogeneticEvolution Phylogenetic
Evolution Phylogenetic
 
Ego net facebook data analysis
Ego net facebook data analysisEgo net facebook data analysis
Ego net facebook data analysis
 
Augmented Reality (AR)
Augmented Reality (AR)Augmented Reality (AR)
Augmented Reality (AR)
 
Client server chat application
Client server chat applicationClient server chat application
Client server chat application
 
Strings in programming tutorial.
Strings  in programming tutorial.Strings  in programming tutorial.
Strings in programming tutorial.
 
Linked list searching deleting inserting
Linked list searching deleting insertingLinked list searching deleting inserting
Linked list searching deleting inserting
 
Number theory
Number theoryNumber theory
Number theory
 
Program to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical orderProgram to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical order
 
Linked list int_data_fdata
Linked list int_data_fdataLinked list int_data_fdata
Linked list int_data_fdata
 
Linked list Output tracing
Linked list Output tracingLinked list Output tracing
Linked list Output tracing
 
Stack
StackStack
Stack
 
Sorting
SortingSorting
Sorting
 
Fundamentals of-electric-circuit
Fundamentals of-electric-circuitFundamentals of-electric-circuit
Fundamentals of-electric-circuit
 
Cyber security
Cyber securityCyber security
Cyber security
 
C programming
C programmingC programming
C programming
 
Structure and union
Structure and unionStructure and union
Structure and union
 
Structure in programming in c or c++ or c# or java
Structure in programming  in c or c++ or c# or javaStructure in programming  in c or c++ or c# or java
Structure in programming in c or c++ or c# or java
 
String
StringString
String
 

Dernier

+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
Health
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
mphochane1998
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Dernier (20)

Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Rums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdfRums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdf
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Learn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic MarksLearn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic Marks
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
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
 

Data structure lecture 1

  • 1. Data Structure 1 Pointer, Function, structure, array, recursive function call
  • 3. Introduction • Pointers – Pointers are variables containing memory addresses of a variable with specific value. – A variable name directly references a value – A pointer indirectly reference a value. – A pointer ‘p’ pointing to a character ‘c’ –
  • 4. Pointer Variable Declarations and Initialization • Pointer variables – Contain memory address as their value – Normal variables contain a specific value (direct reference) – Pointers contain address of a variable that has a specific value (indirect reference) – Indirection – referencing a pointer value • Pointer declarations – * used with pointer variables: e.g., int *myPtr; – Declares a pointer to an int (pointer of type int *) – Multiple pointers require using a * before each variable declaration: int *myPtr1, *myPtr2;
  • 5. – Can declare pointers to any data type – Initialize pointers to 0, NULL, or an address • 0 or NULL –points to nothing (NULL preferred)
  • 6. Calling Functions by Reference • Call by reference with pointer arguments – Pass address of argument using & operator – Allows you to change actual location in memory – Arrays are not passed with & because the array name is already a pointer.
  • 7. Pointer Expressions and Pointer Arithmetic • Arithmetic operations can be performed on Pointers – Increment/decrement pointer (++ or --) – Add an integer to a pointer( + or += , - or -=) – Pointers may be subtracted from each other
  • 8. • 5 element int array on machine with 4 byte int – vPtr points to first element v[ 0 ] – at location 3000 (vPtr = 3000) – vPtr += 2; sets vPtr to 3008 – vPtr points to v[ 2 ] (incremented by 2), but the machine has 4 byte int, so it points to address 3008
  • 9. sizeof Operator • Sizeof – Returns size of operand in bytes – For arrays: size of 1 element *number of elements – sizeof can be used with • Variable names • Type name • Constant values
  • 10. Relationship Between Pointers and Arrays • Arrays and pointers closely related – Array name like a constant pointer – Pointers can do array subscripting operations
  • 11. Pointer-to-pointers • We can have pointers to – int, char, float, any structure…. • Declaration of a pointer-to-pointer: • int **ipp; where two * indicates two level of pointers.
  • 12. • ipp points to ip1 which points to i. • **ipp is i, or 10.
  • 13. Pointers to Functions • Pointer to function – Contains address of function – Similar to how array name is address of first element – Function name is starting address of code that defines Function • Function pointers can be – Passed to functions – Stored in arrays – Assigned to other function pointers
  • 14. Data Structure Books • Text Books: • Mark Allen Weiss - Data Structures and Algorithm Analysis in C, 2nd Edition, Addison-Wesley, 1997 • P.S. Deshpande and O.G. Kakde - C and Data Structures, Charles Rive Media, 2004 • Reference Books: • Data structures and problem solving using C++, By Mark Allen Weiss - Addison-Wesley (2000)