មេរៀនទី៧ Pointer C/C++ for beginner រៀបចំ និងបង្រៀនដោយ៖ អ៊ិន សុភា

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

Call By Address Parameters (Pointers) Chapter 5. Functions that “return” more than a single value What if we need more than one value to be returned from.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Pointers. COMP104 Pointers / Slide 2 Pointers * A pointer is a variable used for storing the address of a memory cell. * We can use the pointer to reference.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
Review 1 List Data Structure List operations List Implementation Array Linked List.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
Pointers and Classes.
Pointers What is the data type of pointer variables?
Pointers and Dynamic Arrays
Chapter 8 Arrays, Strings and Pointers
Pointers & Arrays.
Characters and Strings
Two Dimensional Array Mr. Jacobs.
CSC 113: Computer Programming (Theory = 03, Lab = 01)
Pointers and Pointer-Based Strings
Pointers Revisited What is variable address, name, value?
Pointer.
Pointers Psst… over there.
Andy Wang Object Oriented Programming in C++ COP 3330
Pointers and References
Pointers Psst… over there.
Dynamic Memory Allocation
Pointer Basics Psst… over there.
C Passing arrays to a Function
CS 2308 Exam I Review.
Lecture 12 Oct 16, 02.
មេរៀនទី៥ ការជ្រើសរើសប៉ាន់គំរូ
Pointers Lecture 1 Thu, Jan 15, 2004.
Pointers Pointers are variables that contain memory addresses as their values. A variable name refers to a specific value. A pointer contains an address.
CS150 Introduction to Computer Science 1
C++ Pointers and Strings
C++ Programming Lecture 17 Pointers – Part I
CS150 Introduction to Computer Science 1
Pointers and Pointer-Based Strings
Dynamic Memory.
CS250 Introduction to Computer Science II
C Programming Pointers
Pointers & Arrays.
មេរៀនទី១១ FILE C/C++ for beginner រៀបចំ និងបង្រៀនដោយ៖ អ៊ិន សុភា
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Chapter 9: Pointers and String
A simple function.
Pointers and dynamic objects
Pointer Basics Psst… over there.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Pointers: The Basics.
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

មេរៀនទី៧ Pointer C/C++ for beginner រៀបចំ និងបង្រៀនដោយ៖ អ៊ិន សុភា C# Database C/C++ for beginner មេរៀនទី៧ Pointer រៀបចំ និងបង្រៀនដោយ៖ អ៊ិន សុភា Tel: 016 270 878 / 097 200 13 99 Email: insophea1987@gmail.Com

អ្វីជា Pointer? Pointer ជាvariable ណាមួយដែលផ្ទុក momory address របស់ Variable ណាមួយផ្សេងទៀត, គ្មានផ្ទុកតម្លៃទេ ។ ឧទាហរណ៍ៈ int num1=10; // num1 ជា variable មានតម្លៃស្មើ 10 int *p;​ // declaration ទិន្នន័យជាចំនួនគត់មាន address គឺ p p = &num1;​ // p ទទួលតម្លៃ​ address របស់ num1 ដំបូង Pointer p គ្មានផ្ទុកតម្លៃទេ តែក្រោយពេលមាន assignment p=&num1 នោះ p និង​ point ទៅ num1 ។ ពេលនោះ pointer ជា variable ផ្ទុកតម្លៃ address នៃ memory របស់ num1 ។ ហើយ​ p ខ្លួនឯងក៏ មាន address ផ្ទាល់ខ្លួនដែរគឺ &p ។

Pointer ត្រូវបានប្រើប្រាស់ក្នុងគោលបំណង ៖ Reference(&): គ្រប់គ្រងនូវ Address Dereference(*): គ្រប់គ្រងតម្លៃនៃAddress

Syntax: DataType * pName; Example: int *p=NULL; //ជាទូទៅបើមិនបានបោះ Addressណាមួយទៅឱ្យវាទេ នោះវានឹង point to NULL int num=90; //…. p=&num; //ផ្តល់តម្លៃជា address របស់ num ទៅកាន់ p (គេហៅថា reference) cout<<*p; //ទាញតម្លៃពី address ដែលត្រូវបានគ្រប់គ្រងដោយ p (គេហៅថា deference) DataType * pName;

p point ទៅកាន់ address របស់ num Memory Address Data 0x28fea8 0x28fea4 90 0x28fea0 0x28fe9c 0x28fe98 p 0x28fea4

const Pointer What is difference between them??? const int *pOne; int * const pTwo; const int * const pThree; pOne: is a pointer to a constant Integer. តម្លៃដែលវា point ទៅគឺមិនអាចធ្វើការផ្លាស់ប្តូរបាន​តាម រយៈ pOne បានឡើយ។ Ex: int b=10; const int *pOne= &b; *pOne=40; pTwo: is a constant pointer to an Integer. តម្លៃដែលវា point ទៅគឺអាចធ្វើការផ្លាស់ប្តូរបាន​ ប៉ុន្តែ pTwo គឺមិន អាច point ទៅកាន់ variable ណាមួយផ្សេងទៀតបានឡើយ។ Ex: int a=5,b=10; int * const pTwo= &b; pTwo= &a; pThree: is a constant pointer to a constant Integer.តម្លៃដែលវា point ទៅគឺមិនអាចធ្វើការផ្លាស់ប្តូរបាន​តាម រយៈ pThree បានឡើយ និង pThree គឺមិនអាច point ទៅកាន់ variable ណាមួយផ្សេងទៀតបានឡើយ។ What is difference between them???

លំហាត់ ១). ប្រកាស Number ជាប្រភេទ Integer ជាមួយតម្លៃចាប់ផ្តើម ៥០ ២). ធ្វើការបង្ហាញតម្លៃរបស់ Number មកលើ screen ៣). ធ្វើការបង្ហាញ address របស់ Number មកលើ screen ៤). ប្រកាស Pointer មួយឈ្មោះ pNumber pointing to Number ៥). ធ្វើការបង្ហាញតម្លៃរបស់ pNumber មកលើ screen ៦). ធ្វើការបង្ហាញ address របស់ pNumber មកលើ screen ៧). ផ្លាស់ប្តូរតម្លៃរបស់ Number មក ១០០ បន្ទាប់មកធ្វើលំហាត់ទី២ និង ទី៣ ម្តងទៀត។ ៨). ផ្លាស់ប្តូរតម្លៃរបស់ Number មក ២០០ តាមរយះ pNumber បន្ទាប់មកធ្វើលំហាត់ទី២ និង ទី៣ ម្តងទៀត។

Pointer to Array Name ឬក៏ ឬក៏ -Array គឺជា constant pointer, point ទៅកាន់ធាតុដំបូងបំផុតរបស់ Array។ Ex: int numbers[3]={10,30,20}; cout<<*numbers; //it’s number[0] -របៀបបង្ហាញ Addresses for(int i=0;i<sizeof(numbers)/sizeof(int);i++) cout<<&numbers[i]<<endl; cout<<(numbers+i)<<endl; -របៀបបង្ហាញ Values cout<<numbers[i]<<endl; cout<<*(numbers+i)<<endl; Memory Address Data 0x28fea8 0x28fea4 10 0x28fea0 30 0x28fe9c 20 0x28fe98 ឬក៏ ឬក៏

int numbers[3]={10,30,20}; int *p=numbers; cout<<p<<endl;//pointer to the address of the 1st element =>0x28fea4 cout<<*p<<endl;//1st element =>10 cout<<*(p+1)<<endl;//2nd element => 30 cout<<*p +1<<endl;//? 10 +1 =>11 cout<<*(p+2)<<endl;//3rd element=>20 for(int i=0;i<sizeof(numbers)/sizeof(int);i++) cout<<*(p+i)<<endl; Memory Address 0x28fea8 0x28fea4 10 0x28fea0 30 0x28fe9c 20 0x28fe98

Pointer to an Array V.S an Array of Pointers int numOne[5]; int *numTwo[5]; int *numThree=new int[5]; numOne ជាArray មានធាតុ 5 ដែលធាតុនិមួយៗជា int object ។ numTwo ជា Array មានធាតុ 5 ដែលធាតុនិមួយៗជា pointer ចង្អុលទៅ int object ។ numThree ជា pointer មួយចង្អុលទៅ Array មួយដែលមាន 5 ធាតុនៃ int object ។

លំហាត់ ១). ប្រកាស Array មួយដែលមាន ១០​ ធាតុហើយ initialize តំលៃរបស់វា ២). បង្កើត Pointer ចង្អុលទៅ Array នោះ ៣). ធ្វើការបង្ហាញតំលៃរបស់ធាតុនិមួយៗដោយប្រើប្រាស់ Pointer ៤). រកតម្លៃតូចបំផុតរបស់ Array នោះដោយប្រើប្រាស់ Pointer 5). Sort តម្លៃពីតូចទៅធំរបស់ Array នោះដោយប្រើប្រាស់ Pointer

Pointers and multi-dimensional arrays Ex: int num[2][3]={{10,20,30},{40,50,60}}; -របៀបបង្ហាញ Addresses for(int row=0;row<sizeof(num)/sizeof(num[0]);row++) { for(int col=0;col<sizeof(num[0])/sizeof(int);col++) cout<<&num[row][col]<<"\t"; cout<<(num[row]+col)<<"\t"; cout<<endl; } ឬក៏

Pointers and multi-dimensional arrays Ex: int num[2][3]={{10,20,30},{40,50,60}}; -របៀបបង្ហាញ Values for(int row=0;row<sizeof(num)/sizeof(num[0]);row++) { for(int col=0;col<sizeof(num[0])/sizeof(int);col++) cout<<*(num[row]+col)<<"\t"; cout<<*(*(num+row)+col)<<"\t"; cout<<endl; } Tip: int *p=num;//Because num will not return a pointer to integer. num return a pointer to 1D array of 3 integers. int *p=num[0]; int (*p)[3]=num; //Declaring pointer to 1D array of 3 integers ឬក៏