bab6.ppt

24
POINTER POINTER

Upload: umadevi-balakrishnan

Post on 15-Nov-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

  • POINTER

  • OBJECTIVES6.0 POINTERDefine pointer and explain its functionDeclare pointerIdentify relationship between pointer and arrayIdentify relationship between pointer and functionUse pointer as function argument

  • pointerPointer is a variable whose value is used to point to another variable. A pointer is a variable which means that a pointer has a left value and a right value. The left and right values are addresses. The left value of a pointer is used to refer to the pointer itself whereas the right value of a pointer which is the content of the pointer is the address of another variable.Declaration : Data type specifies the type of data to which the pointer points.Pointer name is the name of the pointer variableAsterik (*) indicates the variable as a pointer.

  • POINT = &ADDRESSVARIABLE POINTERAMPERSANDMEMORY LOCATIONSyntax:data_type *pointer_nameEg:char *ptr_c ; // declare a pointer to a characterint *ptr_int; ; // declare a pointer to a integerfloat *ptr_flt ; // declare a pointer to a floatOperator POINTER:

    CONCEPT OF POINTER

  • The address that locates a variable within memory is what we call a reference to that variable. This reference to a variable can be obtained by preceding the identifier of a variable with an ampersand sign (&), known as reference operator, and which can be literally translated as "address of". For example:

    andy = 25; fred = andy; ted = &andy; The values contained in each variable after the execution of this, are shown in the following diagram: The values contained in each variable after the execution of this, are shown in the following diagram:

  • First, we have assigned the value 25 to andy (a variable whose address in memory we have assumed to be 1776).

    The second statement copied to fred the content of variable andy (which is 25). This is a standard assignment operation, as we have done so many times before. Finally, the third statement copies to ted not the value contained in andy but a reference to it (i.e., its address, which we have assumed to be 1776). The reason is that in this third assignment operation we have preceded the identifier andy with the reference operator (&), so we were no longer referring to the value of andy but to its reference (its address in memory).

  • Pointers are said to "point to" the variable whose reference they store. beth = *ted; we could read as: "beth equal to value pointed by ted") beth would take the value 25, since ted is 1776, and the value pointed by 1776 is 25.

    Notice the difference between the reference and dereference operators: & is the reference operator and can be read as "address of" * is the dereference operator and can be read as "value pointed by"

  • #include int main () { int firstvalue, secondvalue; int * mypointer; mypointer = &firstvalue; *mypointer = 10; mypointer = &secondvalue; *mypointer = 20; cout
  • Example code Pointer

    #include int main () { int firstvalue = 5, secondvalue = 15; int * p1, * p2; p1 = &firstvalue; // p1 = address of firstvalue p2 = &secondvalue; // p2 = address of secondvalue *p1 = 10; // value pointed by p1 = 10 *p2 = *p1; // value pointed by p2 = value pointed by p1 p1 = p2; // p1 = p2 (value of pointer is copied) *p1 = 20; // value pointed by p1 = 20 cout

  • New and delete operatorThe new operator allows the programmer to allocate memory for a specific data type, struct, class, etc, and gives the programmer the address of that allocated sect of memory in the form of a pointer.

    The delete operator is used in front of a pointer and frees up the address in memory to which the pointer is pointing.

  • Example new and delete code#include int main(){int *p1, *p2;

    p1 = new int;*p1 = 142;p2 = p1;cout

  • 3 STEP TO ACCESS POINTER IN struct:

    customer.no_acct;pt->no_acct;(*pt).no_acct;

    ACCESSING OF POINTER

  • EXAMPLE#include #include

    void main() { char t = A;int n = 3333;float b = 99.99; struct {int *no_acct;char *jenis_acct;char *nama;float *baki;} customer, *pt = &customer;

    customer.no_acct = &n;customer.jenis_acct = &t;customer.nama = Siti;customer.baki = &b;

    cout

  • POINTER & ARRAYThe concept of array is very much bound to the one of pointer. In fact, the identifier of an array is equivalent to the address of its first element, as a pointer is equivalent to the address of the first element that it points to, so in fact they are the same concept.

  • POINTER & ARRAYThe elements of an array may be pointers. Here is an array of 4 pointers to type double:double * p [ 4]Its elements can be allocated like any other pointers:p[3] = new double ( 5.1234);

    p01235.1234

  • Program of array pointer#include int main () { int numbers[5]; int * p; p = numbers; *p = 10; p++; *p = 20; p = &numbers[2]; *p = 30; p = numbers + 3; *p = 40; p = numbers; *(p+4) = 50; for (int n=0; n
  • Pointers & Functionto declare a pointer to a function we have to declare it like the prototype of the function except that the name of the function is enclosed between parentheses () and an asterisk (*) is inserted before the nameminus is a pointer to a function that has two parameters of type int. It is immediately assigned to point to the function subtraction

    int (* minus)(int,int) = subtraction;

  • Example pointer function program#include int main() {float min(float,float);float (*ptr)(float,float);float x1,x2, small;ptr = &min;cout > x1 >> x2;small = (*ptr)(x1,x2);cout
  • Pointer & Functions2 types of function:Function pass by referenceFunction pass by value

    Function pass by reference used a pointer as argument

  • Passing arguments by reference#include void int_swap(int*, int*);int main(){ int x=10, y=20;cout
  • excersiceWrite a programme using pointer and function that will accept two numbers from the user and find the total of multiplication that two number

  • Pointer to structuresstructures can be pointed by its own type of pointers:struct movies_t { string title; int year; }; movies_t amovie; movies_t * pmovie;

    Here amovie is an object of structure type movies_t, and pmovie is a pointer to point to objects of structure type movies_t. So, the following code would also be valid: pmovie = &amovie;

  • the arrow operator (->). This is a dereference operator that is used exclusively with pointers to objects with members. This operator serves to access a member of an object to which we have a reference. pmovie->titleWhich is for all purposes equivalent to:(*pmovie).title

    Both expressions pmovie->title and (*pmovie).title are valid and both mean that we are evaluating the member title of the data structure pointed by a pointer called pmovie.

  • The following panel summarizes possible combinations of pointers and structure members:

    ExpressionWhat is evaluatedEquivalenta.bMember b of object aa->bMember b of object pointed by a(*a).b*a.bValue pointed by member b of object a*(a.b)

    *