One-Dimensional%20Arrays - PowerPoint PPT Presentation

About This Presentation
Title:

One-Dimensional%20Arrays

Description:

As an example, if we want to manipulate some grades, we might declare ... Correspondingly, a[i] is equivalent to *(a i) In fact, p[i] is equivalent to *(p i) ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 7
Provided by: hub2
Category:

less

Transcript and Presenter's Notes

Title: One-Dimensional%20Arrays


1
One-Dimensional Arrays
  • Often, programs use homogeneous data. As an
    example, if we want to manipulate some grades, we
    might declare
  • int grade0, grade1, grade2
  • If we have a large number of grades, it becomes
    cumbersome to represent/manipulate the data using
    unique identifiers.
  • Arrays allow us to refer to a large number of the
    same data type using a single name. For
    instance,
  • int grade3
  • Makes available the use of int variables grade
    0, grade1, grade2 in a program. Note that
    arrays are zero-indexed (numbering always starts
    at 0).
  • Now, to access elements of this array, we can
    write gradeexpr, where expr is an integral
    expression.
  • Example (fragment)
  • for( i 0 i lt 3 i )
  • sum gradei

2
Pointers
  • A variable in a program is stored in a certain
    number of bytes at a particular memory location,
    or address, in the machine.
  • Pointers allow us to manipulate these addresses
    explicitly.
  • Two unary operators (inverses)
  • operator address of. Can be applied to any
    variable. Adds a star to type.
  • operator information at. Can be applied
    only to pointers. Removes a star from type

int a 1, b 2, p void void_p char
char_p p a b p
  • An assignment like char_p a is illegal, as
    the types do not match.
  • void is a generic pointer type can make
    assignments such as void_p
    char_p or void_p b

3
Constructs not to be pointed at
  • Do not point at constants
  • int ptr
  • ptr 3 / illegal /
  • Do not point at arrays an array name is a
    constant.
  • int a77
  • void ptr
  • ptr a / illegal /
  • Do not point at expressions that are not
    variables.
  • int k 1, ptr
  • ptr (k 99) / illegal /

4
Call by reference (not really)
  • Pointers allow us to perform something similar to
    call-by-reference (technically, we are passing
    pointers/references by value)
  • call-by-reference allows a function to make
    changes to a variable that persist
  • Examples.

void set_int_to_3( int p ) p 3 / Q
How to call this function? /
void swap( int p, int q ) int temp temp
p p q q temp
5
Arrays and Pointers
  • Assume int i, a10, p
  • The type of a is int .
  • a is equivalent to a0
  • a i is equivalent to ai
  • Correspondingly,
  • ai is equivalent to (a i)
  • In fact,
  • pi is equivalent to (p i)

for( p a p lt a10 p ) sum p
for( i 0 i lt 10 i ) sum (a i)
p a for( i 0 i lt 10 i ) sum pi
6
Example Arrays as Function Arguments(Array
passed by reference, so changes to array persist)
void main() int a5 0, 1, 2, 3,
4 printf( sum of a d\n, change_and_sum(
a, 5 )) printf( value of a0 d\n, a0
) int change_and_sum( int a, int size
) int i, sum 0 a0 100 for( i 0 i
lt size i ) sum ai return sum
Write a Comment
User Comments (0)
About PowerShow.com