t11BArraysStringsAndParameters - Arrays Strings and...

Info icon This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Arrays Strings and Parameter Passing Dale Roberts Character Arrays Character arrays String is really a static array of characters, ex: “first” String “first” Character arrays can be initialized using string literals Character Null character '\0' terminates strings Null '\0' char string1 = "first"; 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', '\ It char string1 'f', 'i', 'r', 's', 't', '\ Can access individual characters Array name is address of array, so & not needed for scanf char Array char string2[20]; char & is NOT used, why? scanf( "%s", string2 ); "%s", string2 Can read a string with max of size 19 and a null character. Can Reads characters until whitespace (space, tab, carriage-return, newline, space, vertical tab) encountered encountered Can write beyond end of array, be careful 0' }; }; string1[ 3 ] is character ‘s’ is Dale Roberts Passing Arrays to Functions Passing arrays To pass an array argument to a function, specify the name of the array To without any brackets int myArray[ 24 ]; int 24 ]; ... myFunction( myArray, 24 ); myArray, 24 ); ... Pass array name Size is also often sent as an argument Array size usually passed to function Array Arrays passed call-by-reference Arrays the called functions can modify the element values in the caller’s original array 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 function modifies array elements in its function body, it is modifying the actual elements of array in the original memory locations actual main() myFunction() myArray … Dale Roberts Passing Arrays to Functions (cont.) (cont.) Passing Example: #include <stdio.h> #include main() { int a[10]; printf(“a = %p \n &a[0] = %p\n”, a, &a[0]); } a = FFEE &a[0] = FFEE output: Passing array elements Passing Individual elements of an array are passed by call-by-value Individual Pass subscripted name (i.e., myArray[ 3 ]) to function Pass myArray[ Example: Example: compare(a[0], a[1]); compare(a[0], 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 int arraySize could be simply int 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 a[ 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; 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 ] ); Function definitions printf( "\n" ); modifyArray( a, SIZE ); /* passed call by reference */ printf( "The values of the modified array are:\n" ); Entire arrays passed call-byreference, and can be modified 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", 3 ] m); odifyElement( 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 37 38 39 40 } 41 42 void modifyElement( int e ) 43 { 44 45 } printf( "Value in modifyElement is %d\n", e *= 2 ); for ( j = 0; j <= size - 1; j++ ) b[ j ] *= 2; int j; Function Function definitions definitions Program Output Effects of passing entire array call by reference: The values of 0123 The values of 0246 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 ...
View Full Document

  • Spring '10
  • kittu
  • Array, ASCII, Dale Roberts

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern