Presentation is loading. Please wait.

Presentation is loading. Please wait.

POINTER I POINTER DECLARATION DECLARATION BY DYNAMIC POINTER.

Similar presentations


Presentation on theme: "POINTER I POINTER DECLARATION DECLARATION BY DYNAMIC POINTER."— 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; Type of data Operator Indicators: For reference the actual value. To show variable is pointer type. Variable

4 POINTER DECLARATION continue… The symbol ‘*’ has a dual role: 1. Declares that the variable is of type pointer. 2. 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; int tahunKursus = 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 DECLARATION continue… There are two ways to declare a pointer: 1. Declaration of the grant using the address operator &. Example: 2. 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 DECLARATION continue… 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 StatementDescription of the contents of memory char grade; int tahunKursus = 2; float CPA = 3.30; 2 3.30 grade tahunKursus CPA 3010 3011 3015 This statement provide value to variable normal and all variable has memory location separate.

9 DESCRIPTION OF THE CONTENTS OF MEMORY StatementDescription of the contents of memory int *P2 = &tahunKursus; float *P3 = &CPA; char *P1; P1 = &grade; 3.30 3010 grade tahunKursus CPA 3010 3011 3015 This statement provide pointers P1, P2, P3 by location memory for the variable the normal change using operator &. Therefore, the pointer P1 and p/u grade, P2 and tahunKursus and P3 and the attributes of a CPA memory location same. 2 3011 3015 P1 P2 P3 3025 3015 3023

10 DESCRIPTION OF THE CONTENTS OF MEMORY StatementDescription of the contents of memory if (*P3 >= 3.0) *P1 = ‘A’; A 3.30 3010 grade tahunKursus CPA 3010 3011 3015 This statement provide value to memory location accused by the P1 and also the p/u grade using pointer operator, ie *. Grant value to the accused who by P3 or the value of CPA variables. 2 3011 3015 P1 P2 P3 3025 3015 3023

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 po inter 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 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 proc esses.

16 MEMORY CONTENTS OF SAMPLES USING OPERATOR new ext... StatementPicture More Memory And Information char *gred; Grade pointer variable is created ? gred

17 MEMORY CONTENTS OF SAMPL ES USING OPERATOR new ext... StatementPicture More Memory And I nformation gred = new gred; Pointer variable grades obtained by t he dynamic memory address for the accused tothe new operator. Memory location can only be filled by the only char. 1021 gred 1021

18 MEMORY CONTENTS OF SAMPL ES USING OPERATOR new ext... StatementPicture More Memory And Information if (gred) : cin>>*gred;The value of memory location pointer accused by grade are included. 1021 gred ‘A’ 1021

19 MEMORY CONTENTS OF SAMPL ES USING OPERATOR new ext... StatementPicture More Memory And Information cout <<“Your Gred :” <<*gred;Output : Your Gred : A Statements that use the operator * This will display the value of the accused by pointing grades.

20 MEMORY CONTENTS OF SAMPL ES USING OPERATOR new ext... StatementPicture More Memory And Information delete gred; Grade pointer variable will s tore the value NULL. gred


Download ppt "POINTER I POINTER DECLARATION DECLARATION BY DYNAMIC POINTER."

Similar presentations


Ads by Google