pointer ii pointer and array pointer and structure

33
Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

Upload: jonas-harris

Post on 18-Jan-2016

277 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

Pointer II

POINTER AND ARRAYPOINTER AND STRUCTURE

Page 2: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

• 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

Page 3: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

POINTER AND ARRAY// reference value with the pointer or array name#include <iostream.h>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 arithmetikcout << 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

Page 4: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

• 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 :1020304050Using an pointer subscript :1020304050Using pointer arithmetic array :1020304050Using pointer arithmetic pointer :1020304050

cout << endl << tata[1];cout << endl << tunjuktata[1];

cout << endl << *(tata +1);cout << endl << *(tunjuktata +1);

Page 5: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

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.

Page 6: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

EXAMPLE PROGRAM#include <iostream.h>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;}

Page 7: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

EXAMPLE OUTPUT

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

Page 8: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

SAMPLE CONTENT FOR USE MEMORY & POINTER AND ARRAY

Statement Picture More Memory And Information

int *number;

?

number

Page 9: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

SAMPLE CONTENT FOR USE MEMORY & POINTER AND ARRAY

Statement Picture 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

number 9765 9766 9767

[0] [1] [2]

Page 10: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

SAMPLE CONTENT FOR USE MEMORY & POINTER AND ARRAY

Statement Picture 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 statementcin>> numbers [i];

9765 45 67 34

number 9765 9766 9767

[0] [1] [2]

Page 11: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

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

Page 12: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

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::

Page 13: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

• 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

Page 14: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

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;

Page 15: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

POINTER TO POINTER

1002 1003 10

Ptp Ptr data

1001 1002 1003

Rajah 2.2.1 : Relationship variable pointer to pointer to data

Page 16: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

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.

Page 17: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

EXAMPLE DECLARATION POINTER AND STRUCTURE.

struct recordStaff{char name[20];char department[50];int year work;float salary;

};recordStaff *p, *q, *while;

Page 18: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

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.

Page 19: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

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;

Page 20: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

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 using 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.

Page 21: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

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.

Page 22: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

STRUCTURE OF MEMORY CONTENTS Pointer

Pengisytiharan penuding

rekodPekerja *p, *q, *sementara;

p q while

?

Page 23: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

STRUCTURE OF MEMORY CONTENTS Pointer

Lending to the memory location pointer

p= new employee records ;

q=new employee records ;

name department Years work salary

name department Years work salary

?

Page 24: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

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; Ahmad JTMK 1999 3500.0

name department Years work salary

?

while

Page 25: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

STRUCTURE OF MEMORY CONTENTS Pointer

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; Asmah JKE 2005 2000.0

name department Years work salary

Page 26: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

STRUCTURE OF MEMORY CONTENTS Pointer

Changing the location of the memory structure of the accused by the pointer p and q.

i) while=p;

Ahmad JTMK 1999 3500.0

name department yearsWork salary

p

while

Page 27: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

STRUCTURE OF MEMORY CONTENTS Pointer

Changing the location of the memory structure of the accused by the pointer p and q.

ii) p=q;Asmah JKE 2005 2000.0

name department Years work salary

q

p

Page 28: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

STRUCTURE OF MEMORY CONTENTS Pointer

Changing the location of the memory structure of the accused by the pointer p and q.

i) q=while;

Ahmad JTMK 1999 3500.0

name department yearsWork salary

while

q

Page 29: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

STRUCTURE OF MEMORY CONTENTS Pointer

Addition of elements connected to the node

name department Years work salary

early

Abdul JKA 2004 3000.5

link

Page 30: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

STRUCTURE OF MEMORY CONTENTS Pointer

Two nodes are connected using a link element

Abdul JKA 2004 3000.5

early

Razak JKM 2003 2800.0

Page 31: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

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.

Page 32: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

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 structure.

• 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

Page 33: Pointer II POINTER AND ARRAY POINTER AND STRUCTURE

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).