ARRAYS, POINTERS, AND STRINGS - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

ARRAYS, POINTERS, AND STRINGS

Description:

1. ARRAYS, POINTERS, AND STRINGS. CHAPTER 6 (PART 1: arrays ... Arrays represent a group of homogenous typed values. grade0. grade1. grade2. grade[0] grade[1] ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 24
Provided by: SEN74
Category:

less

Transcript and Presenter's Notes

Title: ARRAYS, POINTERS, AND STRINGS


1
ARRAYS, POINTERS, AND STRINGS
  • CHAPTER 6
  • (PART 1 arrays and pointers)
  • prepared by Senem Kumova Metin
  • modified by Ilker Korkmaz

2
What is an Array?
  • int grade0, grade1, grade2
  • int grade 3
  • Arrays represent a group of homogenous typed
    values

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

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

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

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 (1) define R 4
define C 5 int xRC
8
Two Dimensional Arrays (2)
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
POINTERS (1)
  • int v 5
  • v is the identifier of an integer variable
  • 5 is the value of v
  • v is the location or address of the v inside the
    memory
  • means the address of
  • Pointers are used in programs to access memory.
  • int v 5
  • int p // p is the identifier of a pointer
    to an integer
  • pv // p is assigned with the address of v
  • p0 // OR p NULL

11
POINTERS (2)
  • int a1, b2, p
  • pa

p
a
b
1
2
??
will point somewhere in memory
p
a
b
1
2
a
12
POINTERS (3)
  • bp
  • // equivalent to ba

a
b
p
1
1 ( the value of a )
a
13
Example 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 pg.250 of the textbook

14
CALL BY VALUE
  • / Whenever variables are passed as arguments to
    a function, their values are copied to the
    function parameters , and the variables
    themselves are not changed in the calling
    environment./
  • int main()
  • int a20 int b30
  • swap (a, b)
  • printf(d d , a, b)
  • return 0
  • void swap(int x, int y)
  • int tmp
  • tmpx
  • xy
  • ytmp
  • return

15
CALL BY REFERENCE
  • / Whenever addresses of variables are passed as
    arguments to a function, their values shall be
    changed in the calling environment./
  • 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
  • return

16
Relationship between pointers and
arrays (1)
  • / The name of an array is the adress or the
    pointer to the first element of the array. /
  • int a5 , p
  • pa // OR pa0

17
Relationship between pointers and
arrays (2)
  • 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

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

19
Pointer Arithmetic
  • EXAMPLE
  • double a2,p,q
  • pa // pa0
  • qp1 // qa01 which means qa1
  • printf(d\n, q-p) / the difference in terms
    of array elements (1 is
    printed)/
  • printf(d\n, (int) q - (int) p) / the
    difference in memory
    locations (8 is printed)/
  • Why 8 ??

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

21
Dynamic Memory Allocation
  • calloc Contiguous memory ALLOCation
  • malloc Memory ALLOCation

22
calloc
  • calloc(n, el_size)
  • an array of n elements, each element having
    el_size bytes
  • void 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 allocated dynamically
    should be returned /

23
malloc
  • / malloc does not initialize memory /
  • void main()
  • int a //will be used as an array
  • int n // size of array
  • printf(give a value for n)
  • scanf(d,n)
  • amalloc(nsizeof(int)) / get space for a
    (allocate a)/
  • ....
  • free(a)
Write a Comment
User Comments (0)
About PowerShow.com