METU Dept. of Computer Eng. Summer 2002 - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

METU Dept. of Computer Eng. Summer 2002

Description:

METU Dept' of Computer Eng' Summer 2002 – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 20
Provided by: ahm4
Category:

less

Transcript and Presenter's Notes

Title: METU Dept. of Computer Eng. Summer 2002


1
Lecture 07
Mon July 22, 2002
  • METU Dept. of Computer Eng. Summer 2002
  • Ceng230 - Section 01
  • Introduction To C Programming
  • by Ahmet Sacan

2
Pointers
  • A pointer is variable which points to a place in
    computer's memory.
  • pointer address
  • pointer variable a variable dedicated to hold
    addresses.

3
and unary operators
  • , when applied to a variable, yields its address
    (pointer to the variable)
  • , when applied to an address (pointer), fetches
    the value at that address.

4
contents .... x107 3 ......
address .... x103 x104 x105 x106 x107 ...
variable name ap a
ip ip a a
x107 3 3 x107
5
Pointer declaration
  • lttypegt ltvarNamegt
  • int iVar / declares an int. /
  • int iVarPtr / declares a variable that
  • will POINT to an integer /
  • int ptr2
  • float f
  • float myFloatPointer

6
Pointer Assignment
  • ltvarNamegt / address of ltvarNamegt /
  • iVarPtr iVar
  • ptr2 iVarPtr
  • myFloatPointer f
  • iVarPtr iVar / BAD... /
  • myFloatPointer iVarPtr / INVALID /

7
NULL
  • a pointer value of 0 is known as NULL pointer.
  • int x
  • x NULL

8
Dereferencing pointers
  • ltptrVarNamegt / value inside the
  • memory
    pointed at /
  • iVar iVarPtr
  • f iVarPtr
  • f myFloatPtr

9
  • int x3, y4, p1NULL
  • int p20
  • p1 x
  • printf("d xd xd d", x, x, p1, p1)
  • x5
  • printf("d xd xd d", x, x, p1, p1)
  • p1 6
  • printf("d xd xd d", x, x, p1, p1)
  • p2 p1
  • printf("xd xd d xd xd d",
  • p1, p1, p1, p2, p2, p2 )

10
Functions Pointers- Call by reference -
  • int square(int x) return xx
  • void main()
  • ...
  • a square(a)

int a void square( ) a aa void main()
... square()
void square(int x) x (x)(x) void
main() ... square(a)
11
int Add(int x, int y) return xy void
main() ... c Add(a, b) ...
void Add(int x, int y, int z) z xy
void main() ... Add(a, b, c) ...
12
  • void OrderSwap ( int x, int y )
  • int t
  • if(x gt y)
  • t x x y y t
  • void main()
  • int a, b, c
  • scanf("d d d", a, b, c)
  • OrderSwap(a,b)
  • OrderSwap(b,c)
  • OrderSwap(a,b)
  • printf("d d d", a, b, c)

13
  • float maxPtr(float xp, float yp)
  • return xp gt yp ? xp yp
  • void main()
  • float a, b, c
  • c maxPtr(a, b)
  • ...

14
Goal
  • First read 10 numbers.
  • Then read numbers 1lt x lt 10 and tell what the
    xth number was.

15
  • int a, b, c, d, e, f, g, h, i, j int x
  • scanf("d d d d d d d d d d d", a, b,
    c, d, e, f, g, h, i, j)
  • scanf("d", x)
  • if(x1) printf("d", a)
  • else if(x2) printf("d", b)
  • ....

16
Arrays
  • int arr 10
  • int i, x
  • for(i0 ilt10 i)
  • scanf("d", arri)
  • scanf("d", x)
  • printf("d", arrx)

17
Arrays
  • array a finite, ordered collection of the
    same-typed data items.
  • element individual data items in an array
  • subscript (index) Only one name is assigned to
    an entire array. Individual elements are
    referenced by specifying an index. Subscripts
    start at ZERO.

18
Array Declaration
  • lttypegt ltarrayNamegt expr-1...expr-n
  • expr-i must be a constant integral type.
  • int midtermScores50
  • define NUM_EXAMS 3
  • float exams3
  • int x 10
  • float pricesx / INVALID!
  • array-expr must be
    constant /

19
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com