1 / 20

POINTER I

POINTER I. POINTER DECLARATION DECLARATION BY DYNAMIC POINTER. INTRODUCTION. Is a data type that stores the address of a value. Allows direct manipulation of data by using the computer’s memory location. To enable the new variables are dynamically created at run time program.

xenos
Download Presentation

POINTER I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. POINTER I POINTER DECLARATION DECLARATION BY DYNAMIC POINTER

  2. INTRODUCTION • Is a data type that stores the address of a value. • Allows direct manipulation of data by using the computer’s memory location. • To enable the new variables are dynamically created at run time program. • Can be used with arrays and structures to refer to a dynamic array structure.

  3. POINTER DECLARATION • General form of the pointer declaration Data_Types*Name_Pointer; • Operator Indicators: • For reference the • actual value . • To show variable is • pointer type. Variable Type of data

  4. POINTER DECLARATIONcontinue… • The symbol ‘*’ has a dual role: • Declares that the variable is of type pointer. • Allows access to the accused by the pointer value is. • Operator ‘&’ is used to give an address to the pointer.

  5. EXAMPLE • Sample declaration and giving the address to the pointer. char grade; inttahunKursus = 2; float CPA = 3.30; int *P2 = &tahunKursus; float *P3 = &CPA; char *P1; P1 = &grade; if (*P3 >= 3.0) *P1 = ‘A’; cout<<“\n The contents of the memory is accused of pointing P1“ << grade; cout<<“\n The contents of the memory is accused of pointing P2“ << *P2; cout<<“\n The contents of the memory is accused of pointing P3“ << *P3;

  6. POINTER DECLARATIONcontinue… • There are two ways to declare a pointer: • Declaration of the grant using the address operator &. Example: • Pointer variable declaration and assignment statement and then use the & operator for the provision of address. Example: int * P2 = &tahunKursus; char *P1; P1 = &grade;

  7. POINTER DECLARATIONcontinue… • Pointer will only point to a memory location that has the same type of data with the data type declaration.

  8. DESCRIPTION OF THE CONTENTS OF MEMORY This statement provide value to variable normal and all variable has memory location separate . grade 3010 tahunKursus 2 3011 CPA 3.30 3015

  9. DESCRIPTION OF THE CONTENTS OF MEMORY This statementprovide pointers P1,P2, P3 by locationmemory for the variablethe normal changeusing operator &.Therefore, the pointer P1and p/u grade, P2 andtahunKursus and P3and the attributes of a CPA memory locationsame. grade 3010 2 tahunKursus 3011 3.30 CPA 3015 3010 P1 3015 3011 P2 3023 3015 3025 P3

  10. DESCRIPTION OF THE CONTENTS OF MEMORY A grade 3010 This statementprovide value tomemory locationaccused by the P1 andalso the p/u gradeusingpointer operator,ie *.Grant valueto the accused whoby P3 or the value ofCPA variables. 2 tahunKursus 3011 3.30 CPA 3015 3010 P1 3015 3011 P2 3023 3015 3025 P3

  11. DECLARATION BY DYNAMIC Pointer • Pointer can also get a dynamic address • Address can be found in the dynamic heap memory address DRP computing is no longer used by any process • Memory management must be done as soon as possible so it can be used optimally.

  12. DECLARATION BY DYNAMIC pointer cont ... • Can be done by using new and delete operators • Dynamic memory allocation can be done with the new operator • If memory is insufficient, the value NULL will be returned. • Therefore, it is important bg ensure every request for a memory location has beensuccessfully done bg avoid NULL pointer value.

  13. EXAMPLES OF THE USE OF OPERATOR new # include <iostream.h> #include <string.h> void main ( ) { char *gred; gred = new gred; if (gred) { cout <<“Pemberian ingatan berjaya”; cout <<“Sila masukkan gred anda”; cin >> *gred; cout <<“Gred anda : ” << *gred; } else cout <<“Pemberian ingatan tidak berjaya”; delete gred; }

  14. EXAMPLES OF THE USE OF OPERATOR new cont ... • New operator will return the address of the computer's memory heap taken DRP kpd p /u grade pointer. • Use the statement if (grade) is to ensure that the pointer got a grade in a dynamicmemory. • If the computer's memory is insufficient, pointing grades will get a NULL value and theimplementation of the program is in the else.

  15. EXAMPLES OF THE USE OF OPERATOR new cont ... • When a dynamically allocated memory is no longer required in the program, it should bereturned to the heap memory • This process of using operator delete • This allows the memory is reused by other programs or processes.

  16. MEMORY CONTENTS OF SAMPLES USING OPERATOR new ext ... gred ?

  17. MEMORY CONTENTS OF SAMPLES USING OPERATOR new ext ... gred 1021 1021

  18. MEMORY CONTENTS OF SAMPLES USING OPERATOR new ext ...MEMORY CONTENTS OF SAMPLES USING OPERATOR new ext ... gred 1021 1021 ‘A’

  19. MEMORY CONTENTS OF SAMPLES USING OPERATOR new ext ...MEMORY CONTENTS OF SAMPLES USING OPERATOR new ext ...

  20. MEMORY CONTENTS OF SAMPLES USING OPERATOR new ext ...MEMORY CONTENTS OF SAMPLES USING OPERATOR new ext ... gred

More Related