C Static Arrays Pepper. What is an array? Memory locations – same type – next to each other...

Post on 12-Jan-2016

218 views 0 download

transcript

C Static Arrays

Pepper

What is an array?• Memory

locations – same type– next to each

other (contiguous)

– Same name – Indexed by

position number of type size_t

– starting at (myarray[2])

Defining Arrays

• Tell the compiler the size and type and give it a name: – int b[10] makes 10 boxes, from 0 to 9

• Initialize later or upon creation: – int b[5] = {33,22,10,11,5};– int a[] = {1,2,3}; - compiler figures out 3 elements

• You can create a variable length array by using variable in place of a number as the length.

Array Structure

• Array variable name holds the starting address• Array can tell where its own self ends• You can tell how many if you know the size of

the variable type

Array Access

Sample program to initialize and print all•Changes to make:– Initialize too few

b[10]– Initialize too many

b[2]– See no crash

#include <stdio.h>int main (void){ int b[5] = { 33, 22, 10,15,5 }; size_t c; for (c = 0; c < 5; c++) { printf ("value at c[%zd] = %d\n", c, b[c]); }}

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Sum an Array

Bubble Sort

• Work from left to right moving one element over until it is lower than one next to it.

• Repeat for as many passes as elements• Once sorted, can do binary search

Bubble Sort

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Bubble Sort

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Bubble Sort

Tricky – use Array item as subscript • Count frequency of survey responses

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Survey cont.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Static Array created inside a function

• Static keyword on array – values will persist if you return to the function

• Static int arrays initialize to 0 upon creation

#include <stdio.h>void arrayInit (void);int main (void){ puts ("first array use"); arrayInit (); puts ("second array use"); arrayInit ();}void arrayInit (void){ static int array1[3];//int array1[3] = {0,0,0}; size_t i; for (i = 0; i < 3; i++) { printf ("array1[ %zd ] = %d\n", i, array1[i] += 5); }}

Get Array Size

• It knows where it starts and where it ends. • Ask array for its total size : – it tells you how many bytes– Divide the number of bytes by the array type

• sizeof(myarray) returns total bytes• sizeof(int) returns size of one integer• Total array elements is then: – sizeof(myarray) / sizeof(myarray[0])

Strings – Character Arrays• Initialize as a string:

– char string1[] = { 'f', 'i', 'r', 's', 't', '\0' };

– char string1[] = "first";

• Access full string as array name– printf(“%s”,string1);

• Access portion with index– printf(“%s”,string1[2]); // will print ‘r’

• The array name does hold an address, so no need for & operator on scanf– char string2[ 20 ]; // remember 1 for ‘\0’– scanf( "%19s", string2 );– Scanf will overwrite the bounds of the array– String2 knows it ends because of the null, but if write over null, array is confused.

Passing Arrays to Functions• Passes Array by reference without & or *– Because array name holds the address– Send the size over as well because it cannot figure out

the size• Passes Array element (scalars) by value– B[1] holds an a variable value, not an address

• Function header:– void arrayfunc (int b[], size_t arrSize)

• Call to function:– arrayfunc(b,10);

• Const If no changes allowed to array elements– void arrayfunc (const int b[], size_t arrSize)

Sample Array pass to function

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Multi-dimension Arrays

• Second subscript : b[4][3]• Initialize: – int b[2][3] = {{1,2,3}, {4,5,6}};

• Function header needs the second number of elements: – void arrayfunc (int b[][3], size_t arrSize)

Ex: Total multidimensional array

Sum = 0// loop through rows of gradesfor ( i = 0; i < pupils; ++i ) { // loop through columns of grades for ( j = 0; j < tests; ++j ) { sum = sum + grades[i][j]; } // end inner for} // end outer for

Comparison to Java

Feature C Java

array declarations int a[5] int[] a = new int[N];

array size

arrays don't know their own size, but you can call sizeof () to get their memory size and divide by the size of one element. Only works on local arrays.

a.length

Summary

• Define an array single or multi subscript• Access an array element• Pass an array as a function – by reference• Get an array size• Special character array• Static keyword for array