pointers structures

Upload: tewodros

Post on 06-Apr-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Pointers Structures

    1/20

    CHAPTER

    POINTERS AND STRUCTURE Hey X Ive ur

    memory address asmy value and I can

    POINT at u and access u whenever

    I want

    X s MEMORY ADDRESS

    X s MEMORYADDRESS

    VALUE

    P X

    P s MEMORY ADDRESS

    P is a pointerwhich points to X

    JU,JIT,ECE, ECE2209

    Jan 20121

  • 8/3/2019 Pointers Structures

    2/20

    Fundamental s

    Poi nter s:A pointer is a var iable that conta ins amemory addre ss.

    point to a location of another variable in memory .

    if P conta ins the addre ss of X,

    then P is sa id to"poi nt t o" X.P is called a po inter

    Pointer declaration

    var_type * var_name int *p ;var_type is the po inter s ba se type , w h ich is a val id

    C++ type , like int , double , float , char .2

    Jan 2012

    JU,JIT,ECE, ECE2209

  • 8/3/2019 Pointers Structures

    3/20

    Fundamental s

    Q1: How to create a pointer p ?A 1: int * P;* used to declare P to be a po inter to an integer

    Q2: How to assign the memory address of X toP?

    A 2: P=&x; // in it ializing a po inter w ith addre ss of x

    & used to a ssign the memory addre ss( locat ion) of X to P

    N.B : Remember to in it ial ize a po inter to po int to amem o ry addre ss of the var iable to be po inted

    3Jan 2012

    JU,JIT,ECE, ECE2209

  • 8/3/2019 Pointers Structures

    4/20

    Fundamental s

    Q 3: How to access the value of X indirectlyusing P ? And give the value of X to Y?

    A 3: normally : Y=X; but, by using p,

    by pointer : Y= *P;

    Assigns the value of X to Y w h ich is po inted by P .S

    o,

    P --- contains the add ress of X*P- -- points to the valu e assigned to X,

    4Jan 2012

    JU,JIT,ECE, ECE2209

  • 8/3/2019 Pointers Structures

    5/20

    Dem onstrat io nLet , i nt X=1 0; and let it is located at thememory locat ion 0x28ff44

    Creat ing a po inter P, i nt *P ;Give the memory locat ion of X to P

    P=&X ; // th is mean s P= 0x28ff44*P po int s to the value of X w h ich is 10i nt Y= *P ; // th is mean s Y=10;

    *P= 20; // th is mean s X=20;

    i.e chang ing *P mean s chang ing X5

    Jan 2012

    JU,JIT,ECE, ECE2209

  • 8/3/2019 Pointers Structures

    6/20

    Example 1#include< iostream>

    using name space std;int ma in(){

    int x;

    int *p;p=&x;

    cout

  • 8/3/2019 Pointers Structures

    7/20

    Example 2#include < iostream>

    using name space std;int ma in(){int balance;int *balptr;

    int value;balance = 3200;balptr = &balance;value = *balptr;cout

  • 8/3/2019 Pointers Structures

    8/20

    Poi nter Arithmet icThe se are the only po ssible operator s

    ++, , +, and

    For an int type computer re serve s 4 byte s and for a char type

    1byteTo increment or decrement the value at the locat ion po intedto by a po inter ,(*p)++; (*p)--; u sing the brac ket s is a mu st for precedencerea son .

    The only po ssible po inter ar ithmet icoperat ion s

    poi nter + integernumber

    poi nter- integernumber

    poi nter- poi nter (the 2 po inter s mu stbe of the same ba se type)

    The se are impo ssible in po interar ithmet ic

    poi nter + f loat(d ouble)number

    poi nter- f loat(d ouble)number

    poi nter +poi nter

    8Jan 2012

    JU,JIT,ECE, ECE2209

  • 8/3/2019 Pointers Structures

    9/20

    exampleT he outcome depends on the size of the object

    pointed to.For example, if the base type is char , one byte isreserved, if it is integer , 4 bytes are reserved.A ssume the pointer int *p if p points to the memory

    address 2000 , the next integer will be at the nextlocation of 2004 because the computer reserves f our bytes for each integer.i.e (*p)++; points to the next integer at the memorylocation of 2004 not at 2001 or

    (*p)--; points to the previous integer at thememory location of 1996 .

    9

  • 8/3/2019 Pointers Structures

    10/20

    Example 3#include< iostream>using name space std;

    int ma in(){char * str="H ELLO"; // assigns H to str because HELLO is an array of chars

    int num[]={10 ,20 ,30 ,40};int *p1= &num[1];

    int *p2= &num[3];int *p3=p1+1; // p3 will point to the address of the memory location of //num[2 ]int n=p2-p1; // note that an integer is represented by 4 bytes. n=2cout

  • 8/3/2019 Pointers Structures

    11/20

    Example 4// demonstrates accessing arrays through pointer arithmetic#includeusing namespace std;int main(){

    const int size = 3;int a[size]={22,33,44};int *end = a+size; // end p oints the fourth element of the array which is nonecout

  • 8/3/2019 Pointers Structures

    12/20

    StructureSometimes it is useful to have a collection of values of different types and totreat the collection as a single item.

    In C++, a structure is a collection of variables that are referenced under onename, providing a convenient means of keeping related information together.Structures are called aggregate data ty p es because they consist of severaldifferent, yet logically connected, variables.T he important property of structures is that the data in a structure can be a

    collection of data items of diverse types.Generally, all members of the structure will be logically related to each other.For example, structures are typically used to hold information such as mailingaddresses, compiler, library card catalog entries, and the like.

    Relationship between members variables are purely determined by the

    programmer.T he keyword str uc t tells the compiler that a structure definition is

    beginning.N otice that the declaration is terminated by a semicolon. T his is

    because a structure declaration is a statement.12

  • 8/3/2019 Pointers Structures

    13/20

    syntax

    13

    struct tag

    {member(s) ;

    }variable ;

    stru ct shop item{ char itemname[3]; // item name

    int itemnum; // item tag number double co st; // cost

    double reta il; // retail priceint onHand; // amount on hand

    }; // structure definition the variables are called members or fields of the structure

  • 8/3/2019 Pointers Structures

    14/20

    Acce ssing structure member sstructure elements are accessed as;

    structure-varname.member-nameFor Eg : to access cost of the structure variable myShop and give it value15.5

    myShop.cost = 15.5;T o print cost on the screen,

    cout

  • 8/3/2019 Pointers Structures

    15/20

    Cont inue .

    The general fo rm of a stru cture de clarat ion is s how n here :Fo r Eg.

    struct struct-type-name{type element_name1 ;type element_name2 ;type element_name3 ;...type element_nameN ;}structure-variables ;

    15

  • 8/3/2019 Pointers Structures

    16/20

    Example 1// a program that store informat ion of student s#include< iostream>using name space std;struct student s

    { int age;int id;char sex;char name[10];};

    int ma in()

    { const int size=10;int n;

    struct student s student1;

    16

  • 8/3/2019 Pointers Structures

    17/20

    Cont inue .cout

  • 8/3/2019 Pointers Structures

    18/20

    Array of structureStructures may be arrayed. In fact, structurearrays are quite common. T o declare an arrayof structures, you must first define a structure,then declare an array of its type.

    For eg : shopitem arrayitem[100];To access a specific structure within an arrayof structures, you must index the structure

    name. For example, to display the c ostmember of the third structure, you wouldwrite,

    cout

  • 8/3/2019 Pointers Structures

    19/20

    Examp e// a program that w ill store student s informat ion for more than one student .#include< iostream>

    using name space std;struct student s

    { int age;int id;

    char sex;char name[10];};

    int ma in()

    { const int size=10;int n;student s mystudent s[size];cout

  • 8/3/2019 Pointers Structures

    20/20

    Cont inuefor (int i=0;i