ARRAYS AND POINTERS - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

ARRAYS AND POINTERS

Description:

... CS115 2005-2006 FALL. 2. What is an Array? int grade0, grade1, grade2; ... Arrays make it possible to represent large number of homogenous values. grade0. grade1 ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 25
Provided by: SEN74
Category:
Tags: and | arrays | pointers | grade1

less

Transcript and Presenter's Notes

Title: ARRAYS AND POINTERS


1
ARRAYS AND POINTERS
  • CHAPTER 6

2
What is an Array?
  • int grade0, grade1, grade2
  • int grade 3
  • Arrays make it possible to represent large number
    of homogenous values

grade0
grade1
grade2
grade0
grade1
grade2
3
One Dimensional Arrays
  • type name_of_arraysize
  • int grade5

MEMORY
data
grade0
first element in array
starts from 0
data
grade1
data
grade2
data
grade3
data
grade4
fift ( the last) element in array
ends at size - 1
4
One Dimensional Arrays
  • int grade5, x9, y
  • grade00
  • grade34
  • grade2-1
  • grade1grade3grade2
  • grade4 x
  • x grade2
  • y gradegrade3

MEMORY
0
grade0
3
grade1
-1
grade2
4
grade3
9
grade4
?
x
?
y
5
INITIALIZATION
  • float f1 1.0, 1.1, 1.2, 1.3 /
    declaration without the size /
  • float f24 1.0, 1.1, 1.2, 1.3 /
    declaration with size /
  • char z abc // char arrays
  • char z a, b, c, \0
  • int a800

6
EXAMPLE One Dimensional Arrays
  • / array1.c/
  • main()
  • int x10, k
  • for(k0klt10k)
  • xkk
  • printf(xd d\n, k, xk)
  • //what will be the output ???

7
Two Dimensional Arrays define R 4 define C
5 int xRC
8
Two Dimensional Array - Memory
9
Two Dimensional Array Initialization
  • int y23 1,2,3,4,5,6
  • int y23 1,2,3,4,5,6
  • int y3 1,2,3,4,5,6

10
Multidimensional Arrays
  • int y23 // two dim.
  • int z 273 // three dim
  • int a223
  • 1,1,0,2,0,0,
  • 3,0,0,4,4,0

11
POINTERS
  • int v 5 is a variable and
  • 5 is the value of it
  • v is the location or address of the v
  • Pointers are declared in programs and then used
    to take addresses !!!
  • int p
  • pv

12
POINTERS
  • int a1, b2, p
  • pa

p
a
b
1
2
??
will point somewhere in memory
p
a
b
1
2
a
13
POINTERS
  • bp
  • // equivalent to ba

a
b
p
1
1 ( the value of a )
a
14
Pointers
  • EXAMPLE
  • include ltstdio.hgt
  • int main(void)
  • int i 7, j , k
  • k i
  • printf("sd\nsp\n", " Value of i ",
    k, "Location of i ", k)
  • jk
  • return 0
  • NOTE table pg.250

15
CALL BY VALUE
  • / Whenever variables are passed as arguments to
    a function, their values are copied to the
    function parameters /
  • void main()
  • int a20 int b30
  • swap (a, b)
  • printf(d d , a, b)
  • void swap(int x, int y)
  • int tmp
  • tmpx
  • xy
  • ytmp

16
CALL BY REFERENCE
  • / Pointers are passed as arguments to a
    function, their
  • addresses are assigned to the function parameters
    defined as
  • pointers/
  • void main()
  • int a20 int b30
  • swap (a, b)
  • printf(d d , a, b)
  • void swap(int x, int y)
  • int tmp
  • tmp x // get value pointed by x.
  • x y // assign value pointed by y to x
  • y tmp

17
Relationship between Pointers and Arrays
  • /An array name by itself is an adress or a
    pointer
  • value /
  • int a5 , p
  • pa0
  • // Equals to
  • pa

18
Relationship between Pointers and Arrays
  • a0 equals to a then a0 equals to a
  • a1 equals to a1 then a1 equals to (a1)
  • a2 equals to a2 then a2 equals to (a2)
  • ai equals to ai then ai equals to (ai)
  • EXAMPLE
  • int a 51,2,3,4,5
  • int p
  • printf(d,a0) printf(d,a)
  • printf(d,a2) printf(d,(a2))
  • pa4
  • Pa4

19
Storage mapping function
  • the mapping b/w pointer values and array indices
  • EXAMPLE
  • int d 3
  • di?(d0i)
  • EXAMPLE
  • int a 3 4
  • aij ? (a004ij)

20
Pointer Arithmetic
  • EXAMPLE
  • double a2,p,q
  • pa // pa0
  • qp1 // qa01 a1
  • printf(d\n, q-p) / difference in terms of
    array elements/
  • printf(d\n, (int) q-(int) p) / difference in
    memory locations/

21
Arrays as Function Arguments
  • main()
  • int x9, r
  • rsum(x,9) //sum(x0,9)
  • double sum( int a, int n)
  • int i
  • double result 0.0
  • for (i0 iltn i)
  • resultai
  • return result
  • main()
  • int x9, r
  • rsum(x,9) // sum(x0,9)
  • double sum( int a, int n)
  • int i
  • double result 0.0
  • for (i0 iltn i)
  • result(ai)
  • return result

22
Dynamic Memory Allocation
  • Calloc contiguous memory allocation
  • Malloc memory allocation

23
CALLOC
  • calloc(n, el_size) ?an array of n elements, each
    element having el_size bytes
  • int main()
  • int a //will be used as an array
  • int n // size of array
  • ....
  • acalloc(n, sizeof(int)) / get space for a ,
    and initialize each bit to zero /
  • free(a) / each space opened dynamically must
    be returned /

24
MALLOC
  • / malloc does not initialize memory /
  • int main()
  • int a //will be used as an array
  • int n // size of array
  • ....
  • amalloc(nsizeof(int)) / get space for a /
  • ....
  • free(a)
Write a Comment
User Comments (0)
About PowerShow.com