Arrays: Strings and Parameter Passing
Arrays: Strings and Parameter Passing
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";
\0
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
myArray
Dale Roberts
output:
#include <stdio.h>
main()
{
int a[10];
printf(a = %p \n &a[0] = %p\n, a, &a[0]);
}
a = FFEE
&a[0] = FFEE
compare(a[0], a[1]);
Function prototype
void modifyArray( int b[], int arraySize );
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
a[
29
30
31
32
/* appears strange */
int main()
{
int a[ SIZE ] = { 0, 1, 2, 3, 4 }, i;
Function definitions
printf( "\n" );
modifyArray( a, SIZE ); /* passed call by reference */
printf( "The values of the modified array are:\n" );
Pass array to a
function
Dale Roberts
33
34 void modifyArray( int b[], int size )
35 {
36
Function
definitions
int j;
37
38
39
40 }
41
42 void modifyElement( int e )
43 {
44
45 }
Program Output
Dale Roberts