8.0 pointer (2)

Upload: cherreralover

Post on 30-May-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 8.0 Pointer (2)

    1/19

    Pointer

  • 8/9/2019 8.0 Pointer (2)

    2/19

    POINTERPointer = a variable that contains memory address ofanother variable as its value

    points to a specific location in memory.

    a pointer represents the address and NOT the valueof a variable.

    The pointer variable can be declared by the followingsyntax

  • 8/9/2019 8.0 Pointer (2)

    3/19

    Fundamentals v is a variable that represents someparticular item. The compiler willautomatically assign memory cells or this

    data item.The data item can then be accessed if weknow the location (an address) of the firstmemory cell.

    the address of vs memory location canbe determined by the expression &v,where & is a unary operator, call theaddress operator, that evaluates theaddress of its operand.

  • 8/9/2019 8.0 Pointer (2)

    4/19

    Fundamentals

    Now let us assign the address of v toanother variable, pv. Thus:

    pv = &v

    This new variable is called a pointer to vsince it points to the location where v is

    stored in memory.Remember, however that pv representsvs address, not its value.

    Thus, pv is referred to as a pointervariable.

  • 8/9/2019 8.0 Pointer (2)

    5/19

    Fundamentals

    The relationship between pv and v is illustrated infigure below

    Data item represented by v can be access by theexpression *pv, where * is a unary operator, calledindirection operator, that operates only on apointer variable. Therefore *pv and v both

    represent the same data item.

    Address of v Value of vpv v

    Relationship between pv and v (where pv = &v and v = *

  • 8/9/2019 8.0 Pointer (2)

    6/19

    Declaring a Pointer

    example:

    int *room;

    char *letter;

    float *pi;

    room, letter

    and pi are pointers

    *room, *letterand *pi are referred toas pointersvalues

  • 8/9/2019 8.0 Pointer (2)

    7/19

    #include /* Assign the add of &suite = 12345*//* Add of room = 12367 */

    void main(){

    int *room; /* room is a pointer */int suite=121; /* suite number is 121 */room=&suite; /* room points to the address

    ofsuite */

    /* Display the value of the pointer room byusing * */

    printf(\nthe room number is %d\n, *room);}

  • 8/9/2019 8.0 Pointer (2)

    8/19

    Pointer Operators

    Using the address operator (&) toindicate the address of its operand

    int *room;

    int suite=121;

    room=&suite;

    Assigns the addresof variable suiteto pointer variable

    room

    It is said that room pointer pointsto suite

  • 8/9/2019 8.0 Pointer (2)

    9/19

    Pointer Operators cont.

    The asterisk (*) is said to returnthe value of object to which the

    pointer points to

    int *room;

    int suite=121,suite2=100;

    room=&suite;

    room=&suite2;

    printf(%d, *room);

    room pointercontains the valuepointed by iti.e. value of

    suite

  • 8/9/2019 8.0 Pointer (2)

    10/19

    int *room;

    - room is the name of pointer (stores address of

    another variable i.e.suite

    )

    - *room is the value found at the address pointed bypointer room

    Tips to remember

  • 8/9/2019 8.0 Pointer (2)

    11/19

    Passing Pointers to aFunction

    Pointers and function:

    The pointer are very much used in a functiondeclaration. Sometimes only with a pointer acomplex function can be easily represented and

    success. The usage of the pointers in a functiondefinition may be classified into two groups.

    1. Call by reference (address or location)

    2. Call by value.

  • 8/9/2019 8.0 Pointer (2)

    12/19

    Pointers and Function

    When an argument is passed by value, thedata item is copied to the function. Thus,any alteration made to the data item withinthe function is not carried over into thecalling routine.

    When an argument is passed byreference, however the address of a dataitem is passed to the function. The contentof that address can be accessed freely,either within the function or within thecalling routine.

  • 8/9/2019 8.0 Pointer (2)

    13/19

    Pointers and Array

    An array is actually very much like pointer.

    We can declare the arrays first element as a[0] or asint *a because a[0] is an address and *a is also an

    address the form of declaration is equivalent.The difference is pointer is a variable and can appearon the left of the assignment operator that is value.

    The array name is constant and cannot appear as theleft side of assignment operator.

  • 8/9/2019 8.0 Pointer (2)

    14/19

    Dynamic MemoryAllocation

    Dynamically allocated variable is a type of variable that doot exist when the program is loaded but it is dynamicallyeated when needed

    ou can use it as many as you like and later on deallocatewhen it is not needed

    By pointing it to a defined structure, you may have a progrITHOUT any variables created

    reserving memory only when you need is called DMA.

  • 8/9/2019 8.0 Pointer (2)

    15/19

    Dynamic Variable Creation

    Creating a pointer will ease up the useof variables defined in a structure

    This pointer is created dynamically and

    later deallocated from the memoryBy having malloc() function, it allows apiece of memory on a heap to beallocated for data and variable

    Deallocating data and variables willcause holes which are then madeavailable to other additional variables

  • 8/9/2019 8.0 Pointer (2)

    16/19

    Heap

    An area in your memory accessibleto store variables

    Using malloc() will reserve certainportion of the heap to the dynamicvariables declared

    These variables can be deallocatedlater on by using free()

  • 8/9/2019 8.0 Pointer (2)

    17/19

    malloc() and free()function

    malloc() allocates memory at the sizeyou want

    void *malloc (size_t size); size = memory size returns the allocated address

    free() will free the memory reserved

    from malloc()void free (void *ptr); ptr = memory address to be freed

  • 8/9/2019 8.0 Pointer (2)

    18/19

    Exercise

    Modify the program that illustrate therelationship between two integer variables asfollows:

    Use floating-point data rather than integerdata. Assign an initial value of 0.3 to u. Use double-precision data rather than

    integer data. Assign an initial value of 0.3 x1045 to u.

    Use character data rather than integer data.Assign an initial value of C to u.

    Be sure to modify the printf statementaccordingly

  • 8/9/2019 8.0 Pointer (2)

    19/19

    Exercise

    Modify the program example from passingpointers to a function, so that a single one-dimensional, character-type array is passed tofunct1. Delete funct2 and all references tofunct2. Initially, assign the string red to thearray within main. Then reassign the stringgreen to the array within funct1. Execute theprogram and compare the results. Remember

    to modify the printf statements accordingly.