Pointers - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Pointers

Description:

Referencing a value through a pointer is called indirection. ... The * operator indirection or dereferencing operator, returns the value of the ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 11
Provided by: Gues355
Category:

less

Transcript and Presenter's Notes

Title: Pointers


1
Pointers
  • Pointers are variables that contain memory
    addresses as their values.
  • A variable directly contains a specific value.
  • A pointer contains an address of a variable that
    contains a specific value.
  • A variable name directly references a value.
  • A pointer indirectly references a value.
  • Referencing a value through a pointer is called
    indirection.

2
Directly and Indirectly referencing a variable
  • sum
  • sumPtr sum

sum directly references a variable whose value
is 5
5
sumPtr indirectly references a variable
whose value is 5
5
3
Initialization
  • A pointer may be initialized to 0 , NULL or an
    address.
  • NULL is a symbolic constant defined in ltstdio.hgt
  • A pointer with the value NULL points to nothing.
  • The only integer that can be assigned to a
    pointer is 0.
  • Dereferencing
  • The operator indirection or dereferencing
    operator, returns the value of the object that
    its operand points to in memory. This is called
    dereferencing the pointer.

4
/ Program to display the address and value of a
pointer /
  • include ltstdio.hgt
  • int main()
  • int a
  • int aPtr
  • a 200
  • aPtr a
  • printf(The address of a is p \n, a)
  • printf(The value of aPtr is p \n, aPtr)
  • printf(The value of a is d \n, a)
  • printf(The value of aPtr is d \n,
    aPtr)

5
Calling functions by reference
  • include ltstdio.hgt
  • void cubebyref( int )
  • int main()
  • int num 5
  • printf(The number is d \n,num)
  • cubeByRef(num)
  • printf(The new value of number is d
    \n,num)
  • return 0
  • void cubeByRef(int numPtr)
  • numPtr nPtr nPtr nPtr

6
Relationship between Pointers and Arrays
  • An array name is the address of the first element
    of an array.
  • int b5
  • The address of the first element
  • using p to print - b, b0, b.
  • Assume that int b5 and pointer variable bPtr
    have been declared. Since the array name is a
    pointer to the first element of the array
  • bPtr b
  • This is equivalent to bPtr b0
  • Array element b1 can be referenced by
  • ( bPtr 1)
  • 1 is the offset to the pointer.
  • The preceding notation is called pointer / offset
    notation.

7
  • In pointer/offset notation, the offset is the
    same as the array subscript.
  • Pointers can be subscripted exactly as arrays
    can.
  • bPtr1 refers to the array element b1.
  • This is called pointer / subscript notation.

8
  • include ltstdio.hgt
  • int main()
  • int b 10 , 20 , 30 , 40
  • int bPtr b
  • int i, offset
  • printf(Pointer/offset notation where the
    pointer is )
  • printf(the array name \n)
  • for(offset 0 offset lt 4 offset)
  • printf((b d) d \n, offset, ( b
    offset) )
  • printf(Pointer/subscript notation \n)
  • for( i 0 i lt 4 i)
  • printf(bPtr d d \n, i, bPtr i )
  • printf(pointer/offset notation \n)

9
  • include ltstdio.hgt
  • void copy( char , const char )
  • int main()
  • char string110, string2 CS230
  • char string310, string4 Lab
  • copy(string1, string2)
  • printf(string1 s \n,string1)
  • copy(string3,string4)
  • printf(string3 s \n,string3)
  • return 0

10
  • void copy(char s1, const char s2)
  • int i 0
  • do
  • s1i s2i
  • i
  • while (s2i ! \0)
  • s1i \0
Write a Comment
User Comments (0)
About PowerShow.com