CSci 160 Lecture 40 - PowerPoint PPT Presentation

About This Presentation
Title:

CSci 160 Lecture 40

Description:

CSci 160 Lecture 40 Martin van Bommel lvalues In C, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment ... – PowerPoint PPT presentation

Number of Views:87
Avg rating:3.0/5.0
Slides: 12
Provided by: Martin1067
Category:

less

Transcript and Presenter's Notes

Title: CSci 160 Lecture 40


1
CSci 160Lecture 40
  • Martin van Bommel

2
lvalues
  • In C, any expression that refers to an internal
    memory location is called an lvalue
  • Appear on left side of assignment statement
  • e.g. x 1.0 intarray2 17
  • Constants expressions are not lvalues

3
Memory and Data Representation
  • Every lvalue is stored somewhere in memory and
    therefore has an address
  • Once declared, the address of an lvalue never
    changes, even though its contents may change
  • Depending on type of data, different lvalues
    require different amounts of memory
  • The address of an lvalue is itself data that can
    be manipulated and stored in memory

4
Pointer Declaration
  • A pointer is a variable storing the address of
    another variable
  • Declaration type ptr
  • Example int p1, p2
  • p1 and p2 are referred to as pointers-to-int, and
    may contain the addresses of integers

5
Pointer Operations
  • C defines two operators that manipulate pointer
    values address-of value-pointed-to
    (dereference)
  • Operand of must be lvalue, and returns the
    address of the lvalue
  • Operand of must be a pointer, and returns the
    value pointed to by the pointer

6
Special pointer NULL
  • NULL is the value of a pointer that does not
    currently point to any data
  • Can not dereference NULL pointer
  • Exists for checking file pointers, and for
    applications we will see later
  • infile fopen(file.txt, r)if (infile
    NULL)
  • printf(File not open)

7
Pass by Reference
  • To modify value of argument, use pointer
  • void SetToZero(int ip)
  • ip 0
  • which makes ip a pointer to the location of
    the value of the argument of the call
  • SetToZero(x)

8
Advantage of use of operator
  • When calling a function using
  • func(x)
  • you know the value of the variable x will not
    change, whereas calling a function using
  • func(x)
  • is allowed to change the value of x
  • Easier to predict effects of function call

9
SwapInteger function
  • When sorting arrays, wanted to use
  • SwapIntegers(arraylow, arrayhigh)
  • Using pointers allows the following
  • void SwapIntegers(int p1, int p2)
  • int temp
  • temp p1
  • p1 p2
  • p2 temp

10
Returning Multiple Results
  • Convert time (minutes) to hours and minutes
  • void ConvertTime(int time,
  • int hours,
  • int mins)
  • hours time / 60
  • mins time 60

11
Better to use pure functions
  • int Hours(int time)
  • return (time / 60)
  • int Minutes(int time)
  • return (time 60)
  • printf(d02d\n,Hours(time),Minutes(time))
Write a Comment
User Comments (0)
About PowerShow.com