Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointer II POINTER AND ARRAY POINTER AND STRUCTURE.

Similar presentations


Presentation on theme: "Pointer II POINTER AND ARRAY POINTER AND STRUCTURE."— Presentation transcript:

1 Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

2 Name of the array is a pointer pointing to wrote the first element of the array. In array attributes using the array name or pointer int nom[5] = {10, 20, 30, 40, 50}; nom [0] will get value 10 int *tunjuknom = nom; *nom mean value of 10 will be referred to POINTER AND ARRAY

3 // reference value with the pointer or array name #include void main() {int a, tata[5]={10,20,30,40,50}; int *tunjuktata; tunjuktata=tata; cout <<“Using an array subscript :”<< endl; for (a=0; a<5; a++) cout <<tata[a]; cout << endl <<“Using an pointer subscript :”<< endl; for (a=0; a<5; a++) cout <<tunjuktata[a]; //pointer arithmetik cout << endl <<“Using pointer arithmetic array :”<< endl; for (a=0; a<5; a++) cout << *(tata + a); cout << endl <<“Using pointer arithmetic pointer :”<< endl; for (a=0; a<5; a++) cout << *(tunjuktata + a); } Program 2.2.1

4 Output aturcara 2.2.1 Exercise : if the following statement be added in the 2.2.1 software, the output of the program said. POINTER AND ARRAY Using an array subscript : 1020304050 Using an pointer subscript : 1020304050 Using pointer arithmetic array : 1020304050 Using pointer arithmetic pointer : 1020304050 cout << endl << tata[1]; cout << endl << tunjuktata[1]; cout << endl << *(tata +1); cout << endl << *(tunjuktata +1);

5 POINTER AND ARRAY pointer can point a collection of data such as arrays and structures Example Declaration : int *number = new int[5]; The use of the array allows the user to freely enter the number of data.

6 EXAMPLE PROGRAM #include int main () { int *number; int i, size; float coverage, total; cout<<“The number of numbers to be included?” ; cin>> size; nombor = new int[size]; for (i=0; i<size; i++) { cout<<“\n Number : “; cin>>number[i]; jumlah+=number[i]; } coverage = total/size; cout<<“\n Number you enter is : “; for (i=0; i<size; i++) cout<<“\n”<<number[i]; cout<<“\n The average of the numbers included : “<<coverage; return 0; }

7 EXAMPLE OUTPUT The number of numbers to be included? 3 Number : 45 Number : 67 Number : 34 Number you enter is : 45 67 34 The average of the numbers included : 48.667

8 SAMPLE CONTENT FOR USE MEMORY & POINTER AND ARRAY StatementPicture More Memory And Information int *number; ? number

9 SAMPLE CONTENT FOR USE MEMORY & POINTER AND ARRAY StatementPicture More Memory And Information number = new int[size]; According to the output of a / c, the input size will return the new value 3.Statement three succesive address or pointer array size and number three will storing the first address of the site. 9765 number976597669767 [0][1][2]

10 SAMPLE CONTENT FOR USE MEMORY & POINTER AND ARRAY StatementPicture More Memory And Information For (i=0; i<size; i++) { cout<<“\n Number :”; cin>>number[i]; total+=number[i]; } According to the output of a / c, three inputs of the array is 45.67 and 34,input is inserted through the loop with the statement cin>> numbers [i]; 9765456734 number976597669767 [0][1][2]

11 MEMORY MANAGEMENT : OPERATOR NEW AND DELETE Using the array with the elements who remain, for example: int procedure [100]; This array provides a total of 100 spaces for integer values. array is useful for storing data, there is plenty of room used Posted to fill the array size is known size Prev program compile compiler for camp provides ample space reply. To avoid the wasteful use of memory, it is better to use Dynamic memory allocation is required when running a program

12 OPERATOR NEW New operator to obtain the memory of the computer and return the pointer to commencement address Format from statements to obtain a memory heap pointer assign said to new key followed by the type of data and numbers. pointer = new jenis_data [number]; Pointer types must be equal to jenis_data. Example: int *pmem; pmem = new int [100]; In effect the same as declaring an array of good [100], except that the memory obtained from the heap. Examples of programs::

13 Delete operator returns the memory to a computer and allows the memory used for purposes other reply Format statement to return the memory to the heap is the keyword delete followed by the name of the pointer. delete name_pointer; OPERATOR DELETE

14 POINTER TO POINTER Keep a constant or variable value, while the pointer variable store the address of another variable. Pointer to a pointer variable is a variable store address for that else pointer variables How to declare pointer variables is to use a pointer to symbol "**" before the variable. Format declaration as follows: - jenis_data **pointertopointer; example : int ** Ptp; int *Ptr, data=10; Ptr=&data; Ptp=&ptr;

15 POINTER TO POINTER 1002100310 PtpPtr data 10011002 1003 Rajah 2.2.1 : Relationship variable pointer to pointer to data

16 POINTER AND STRUCTURE For the use of pointers and structures, the addition should be done structure pointer declaration Declaration of the basic structure is as discussed earlier. An example program below shows the declaration of the pointer p, q, and temporary.

17 EXAMPLE DECLARATION POINTER AND STRUCTURE. struct recordStaff{ char name[20]; char department[50]; int year work; float salary; }; recordStaff *p, *q, *while;

18 Pointer AND STRUCTURE When the pointer structure has been declared,the operation can be performed on the elements of the structure. There are two ways to refer to elements of the structure pointer: that the operator * oruse the ->. Use operator * operator is used with brackets.

19 Pointer AND STRUCTURE The first way to refer to structure elements of the name of the pointer p is as follows: (*p).name; For the second method using operator - > statement is as follows: p->name;

20 Pointer AND STRUCTURE Besides that references to the elements, pointer structures can also be changed pointer. The contents contained in structure pointer can also be assigned to the content of otherstructure pointer us ing the operator *. Example: *p = *q; This statement shows the content of the accused by the pointer q assigned to thecontent of the structure pointer p.

21 Pointer AND STRUCTURE Pointer structures can also be sent to the function, it can only be done in the delivery address or a reference value.

22 STRUCTURE OF MEMORY CONTENTS Pointer Pengisytiharan penuding rekodPekerja *p, *q, *sementara; p q while ?

23 STRUCTURE OF MEMORY CONTENTS Pointer Lending to the memory location pointer p= new employee records ; q=new employee records ; name department Years worksalary name department Years worksalary ?

24 STRUCTURE OF MEMORY CONTENTS Pointer The first element refers to the ways and means bothReferring to elements of the structure pointer p strcpy((*p).name “Ahmad”); strcpy((*p).department, “JTMK”); (*p).years work=1999; (*p).salary=3500.0; AhmadJTMK19993500.0 namedepartmentYears worksalary ? while

25 STRUCTURE OF MEMORY CONTENTS Poin ter The first element refers to the ways and means both. ii) Referring to elements of the structure pointer q strcpy(q->name, “Asmah”); strcpy(q->department, “JKE”); q->years work=2005; q->salary=2000.0; AsmahJKE20052000.0 name department Years worksalary

26 STRUCTURE OF MEMORY CONTENTS Pointer Changing the location of the memory structure of the accused by the pointer p and q. i) while=p; AhmadJTMK19993500.0 namedepartment yearsWorksalary p while

27 STRUCTURE OF MEMORY CONTENTS Pointer Changing the location of the memory structure of the accused by the pointer p and q. ii) p=q; AsmahJKE20052000.0 namedepartment Years worksalary q p

28 STRUCTURE OF MEMORY CONTENTS Pointer Changing the location of the memory structure of the accused by the pointer p and q. i) q=while; AhmadJTMK19993500.0 namedepartment yearsWorksalary while q

29 STRUCTURE OF MEMORY CONTENTS Pointer Addition of elements connected to the node namedepartment Years worksalary early AbdulJKA20043000.5 link

30 STRUCTURE OF MEMORY CONTENTS Pointer Two nodes are connected using a link element AbdulJKA20043000.5 early RazakJKM20032800.0

31 POINTER AND STRUCTURE If you see the contents of memory, the pointer structure of p and q points to the structure of their better known as a node. Thus when a hundred employee information is entered, hundred pointer structures should be created. This is not an efficient way to use the structure pointer.

32 POINTER AND STRUCTURE To overcome this problem, an element in the structure should be established to connect the structure with a pointer pointing to another struct ure. According to the diagram of the contents of memory, an element called node link was used and was accused by the initial pointer. To enable access to data made available on the structure pointer, only a pointer used to point to the node before

33 POINTER AND STRUCTURE It can be seen as a wagon be seen as a wagon train locomotives and rolling stock-wagons in tow are contiguous to each other. Implementation of contiguous nodes is known as a linked list (will be studied in the next chapter).


Download ppt "Pointer II POINTER AND ARRAY POINTER AND STRUCTURE."

Similar presentations


Ads by Google