200 likes | 368 Views
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.
E N D
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. • Can be used with arrays and structures to refer to a dynamic array structure.
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
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.
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;
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;
POINTER DECLARATIONcontinue… • Pointer will only point to a memory location that has the same type of data with the data type declaration.
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
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
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
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.
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.
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; }
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.
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.
MEMORY CONTENTS OF SAMPLES USING OPERATOR new ext ... gred ?
MEMORY CONTENTS OF SAMPLES USING OPERATOR new ext ... gred 1021 1021
MEMORY CONTENTS OF SAMPLES USING OPERATOR new ext ...MEMORY CONTENTS OF SAMPLES USING OPERATOR new ext ... gred 1021 1021 ‘A’
MEMORY CONTENTS OF SAMPLES USING OPERATOR new ext ...MEMORY CONTENTS OF SAMPLES USING OPERATOR new ext ...
MEMORY CONTENTS OF SAMPLES USING OPERATOR new ext ...MEMORY CONTENTS OF SAMPLES USING OPERATOR new ext ... gred