Arrays - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Arrays

Description:

One dimensional arrays are declared exactly the same way as they are in Java. ... Arrays as Function Parameters ... declare it as a pointer as in the second ... – PowerPoint PPT presentation

Number of Views:67
Avg rating:3.0/5.0
Slides: 15
Provided by: alanb9
Category:
Tags: arrays | as

less

Transcript and Presenter's Notes

Title: Arrays


1
Arrays Strings
  • Chapter 15

2
One Dimensional Arrays
  • One dimensional arrays are declared exactly the
    same way as they are in Java.
  • const short MAX_SIZE10long varMAX_SIZE
    //declares an array of longs //with 10
    elements
  • Arrays elements are 0 MAX_SIZE-1
  • Using an array is as simple as var255
  • You should always use a constant to specify the
    size of your arrays. It will allow you to change
    the size of your array easily in one place as
    opposed to changing a value in multiple places
    across your code.

3
Whats going on behind the scenes?
  • When you create an array, C allocates a
    contiguous block of memory large enough for the
    entire array, then creates a constant pointer
    that points to the first memory address in the
    reserved block.
  • Since the array is a constant pointer, you cant
    change the address that the pointer points to,
    but you can change the data in the memory
    location being pointed at.
  • When you access an element of an array, C
    calculates the memory address that the element
    resides in.
  • Base_address (element_ size_of_type)

4
Things to keep in mind
  • C doesnt perform any bounds checking. It will
    happily allow you to use any subscript value even
    if it is well outside of 0 MAX_SIZE-1. You
    will have to make sure your programs never will
    operate outside of the allowed element range.
    Going out of bounds will result in data
    corruption and/or program/OS crashes.
  • Try not to make your arrays much larger than they
    need to be. When you start your program, it will
    automatically grab all of the memory necessary
    for your arrays. If they are much larger than
    they need to be, then they are needlessly wasting
    memory.

5
Another way to do things
  • Using pointers instead of array subscripts
  • Anything that can be accomplished with an array
    subscript can also be accomplished with a
    pointer, and the pointer version will be faster.
  • Subscripts really arent necessary in C, but they
    are provided as a convenience feature in the
    background, they are just dereferencing a
    pointer.
  • To use pointers instead of subscripts, you need
    to understand pointer arithmetic.

6
Pointer Arithmetic
  • Pointer arithmetic is the process of adding
    integer values to pointers. When you add an
    integer value to a pointer, it doesnt just add
    the integer, it adds the value of the integer
    times the size of the type of data being pointed
    at.
  • Lets assume we have declared an array of longs,
    which starts at memory address 150, and a long is
    4 bytes. If we add 5 to the pointer value,
    instead of being 155, the address calculated
    would be 150 (54) which is 170.
  • This means that you dont consciously need to
    account for the size of the type of data youre
    using, C will do it for you, and whenever you do
    pointer arithmetic, it will always point you at
    the beginning of the next element in your array.
    If it didnt, and you added 5 to your pointer
    like we just did, it would point you at the
    second byte of the second element in your array,
    rather than pointing at the beginning of the 5th
    element.

7
How do we use pointer arithmetic?
  • First, you must remember that an array is really
    a constant pointer, so you cant directly perform
    pointer arithmetic on it. You first need to
    create a non-constant pointer to work with.
  • double list20double pplist
  • Now you can use p to advance to the next
    element in your array.

8
Pointer Arithmetic Example
  • The first way
  • const short MAX 20double listMAXdouble
    pfor( plist pltpMAX p)p 0
  • The other way
  • const short MAX 20double listMAXfor(int
    c0 cltMAX c)(listc) 0
  • While this is a perfectly correct way of using
    pointer arithmetic to access array elements, it
    is effectively doing all of the operations that
    would normally be done behind the scenes when
    using subscript operators, and is thus no more
    efficient.

9
Arrays as Function Parameters
  • Arrays cannot be passed by value, they can only
    be passed by reference
  • When passing an entire array, you only need to
    specify the name of the array and dont need to
    add the operand. Since an array is a pointer,
    and thus a memory address, the operator is
    redundant. You also need to pass the size of
    your array as a second parameter so that your
    function can keep inside the bounds of the array.
  • print_list( list, MAX )

10
Arrays as Function Parameters
  • There are two ways to accept arrays as parameters
    to your functions.
  • int print_list( double l , short size )
  • int print_list( double l, short size )
  • Either way is perfectly acceptable. If you use
    the subscript operators, you dont need to
    specify the size of the array because its not
    creating a copy of the array, and thus doesnt
    need to allocate new memory. If you declare it
    as a pointer as in the second example above, you
    can use pointer arithmetic directly on the passed
    variable because it is not a constant pointer.

11
Array Elements as Parameters
  • To send a single element to a function, you would
    do it exactly the same way as you would send a
    single variable to a function.
  • scanf(ld, list3)

12
A Warning About Returning Arrays
  • While you are not allowed to return arrays
    directly because they arent scalar, you are
    allowed to return pointers to them. NEVER return
    pointers to arrays that were created in the
    function, and that werent declared static, or
    allocated dynamically via malloc. Arrays
    declared in functions without being declared
    static or via malloc are considered auto
    variables and the memory that was allocated for
    them will be freed after the function returns.
    The next function is likely to overwrite the
    memory because it will be using the same stack
    space.
  • There is no reason you cant return pointers to
    static arrays created in a function, but its
    considered bad practice. The array may be
    changed during the next call to the function, and
    theres no way to tell the calling function that
    the array has been changed.

13
Multi Dimensional Arrays
  • Multi dimensional arrays are simply arrays of
    arrays, and are sometimes thought of as tables
    with rows and colums. They are declared as
    follows
  • double var MAX_ROWMAX_COL
  • When passing multi dimensional arrays to
    functions, you may omit the number of rows from
    the parameter definition, but you MUST specify
    the number of columns in your parameter
    definition so that your function can do the
    proper address calculations.
  • int print_multi( double list MAX_COL,
    MAX_ROW)
  • For more information about multi dimensional
    arrays and how to use pointer arithmetic with
    them, see chapter 15 section 8.l

14
Strings
  • Strings in C are arrays of characters
  • You already know you can specify a string
    constant using single quotes ie printf(hello)
  • char outputMAX_SIZE
  • char output hello world
  • Strings are terminated with the null character \0
  • scanf(s,output) //will only store string up
    to // first white space
  • fgets(output, MAX_SIZE, stdin) //will store an
    //entire string, including white space
  • Predefined string functions can be found in
    string.h
Write a Comment
User Comments (0)
About PowerShow.com