Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Arrays Dale Roberts, Lecturer IUPUI [email protected] Strings and Parameter Passing Dale Roberts.

Download Report

Transcript Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Arrays Dale Roberts, Lecturer IUPUI [email protected] Strings and Parameter Passing Dale Roberts.

Department of Computer and Information Science,
School of Science, IUPUI
CSCI 230
Arrays
Dale Roberts, Lecturer
IUPUI
[email protected]
Strings and
Parameter Passing
Dale Roberts
Character Arrays
Character arrays
String is really a static array of characters, ex: “first”
Character arrays can be initialized using string literals
char string1[] = "first";
Null character '\0' terminates strings
f
i
r
s
t
\0
string1 actually has 6 elements Null character (indicates string termination)
It is equivalent to char string1[] = { 'f', 'i', 'r', 's', 't',
'\0' };
Can access individual characters
string1[ 3 ] is character ‘s’
Array name is address of array, so & not needed for scanf char
char string2[20];
& is NOT used, why?
scanf( "%s", string2 );
Can read a string with max of size 19 and a null character.
Reads characters until whitespace (space, tab, carriage-return, newline,
vertical tab) encountered
Can write beyond end of array, be careful
Dale Roberts
Passing Arrays to Functions
Passing arrays
To pass an array argument to a function, specify the name of the array
without any brackets
int myArray[ 24 ];
...
myFunction( myArray, 24 );
...
Pass array name
Size is also often sent as an argument
Array size usually passed to function
Arrays passed call-by-reference
the called functions can modify the element values in the caller’s original
array
Name of array is the address of first element of the array
Function knows where the array is stored. Therefore, when the called
function modifies array elements in its function body, it is modifying the
actual elements of array in the original memory locations
main()
myFunction()
…
myArray
Dale Roberts
Passing Arrays to Functions (cont.)
Example:
output:
#include <stdio.h>
main()
{
int a[10];
printf(“a = %p \n &a[0] = %p\n”, a, &a[0]);
}
a = FFEE
&a[0] = FFEE
Passing array elements
Individual elements of an array are passed by call-by-value
Pass subscripted name (i.e., myArray[ 3 ]) to function
Example:
compare(a[0], a[1]);
will be passed by value
An array is a vector while individual elements are scalars.
Function prototype
void modifyArray( int b[], int arraySize );
Parameter names optional in prototype
int b[] could be written int []
int arraySize could be simply int
Dale Roberts
1
2
*/
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
);
29
30
31
32
/* Fig. 6.13: fig06_13.c
Passing arrays and individual array elements to functions
#include <stdio.h>
#define SIZE 5
void modifyArray( int [], int );
void modifyElement( int );
/* appears strange */
int main()
{
int a[ SIZE ] = { 0, 1, 2, 3, 4 }, i;
Function definitions
printf( "Effects of passing entire array call "
"by reference:\n\nThe values of the "
"original array are:\n" );
for ( i = 0; i <= SIZE - 1; i++ )
printf( "%3d", a[ i ] );
Entire arrays passed call-byreference, and can be modified
printf( "\n" );
modifyArray( a, SIZE ); /* passed call by reference */
printf( "The values of the modified array are:\n" );
for ( i = 0; i <= SIZE - 1; i++ )
Array elements passed call-byprintf( "%3d", a[ i ] );
value, and cannot be modified
printf( "\n\n\nEffects of passing array element call "
"by value:\n\nThe value of a[3] is %d\n", a[ 3 ]
modifyElement( a[ 3 ] );
printf( "The value of a[ 3 ] is %d\n", a[ 3 ] );
return 0;
Pass array to a
function
Pass array element
to a function
}
Print
Dale Roberts
33
34 void modifyArray( int b[], int size )
35 {
36
Function
definitions
int j;
37
38
39
for ( j = 0; j <= size - 1; j++ )
b[ j ] *= 2;
40 }
41
42 void modifyElement( int e )
43 {
44
printf( "Value in modifyElement is %d\n", e *= 2 );
45 }
Program Output
Effects of passing entire array call by reference:
The values of
0 1 2 3
The values of
0 2 4 6
the original array are:
4
the modified array are:
8
Effects of passing array element call by value:
The value of a[3] is 6
Value in modifyElement is 12
The value of a[3] is 6
Dale Roberts