CS 108 Computing Fundamentals October/November 2017 Array Bootcamp
For arrays: passing to a function "by value" means passing a single element's "contents" For arrays: no more than one element's contents can be sent to a function if passed "by value" For arrays: "passing by value" means one element's contents passed to the function at-a-time Remember: "passing by value" means a COPY of an element's value is sent to the function... the original value is untouchable Here's an example. #include <stdio.h> // 1.c void show ( int ) ; int grades [ 8 ] = 76, 89, 92, 55, 68, 82, 86, 79 ; for ( i = 0 ; i <= 7 ; i = i + 1 ) show ( grades [ i ] ) ; void show ( int p_grade ) printf ( "%d\n", p_grade ) ; return ;
The next program demonstrates that the original values are untouchable #include <stdio.h> // 2.c void show ( int ) ; int grades [ 8 ] = 76, 89, 92, 55, 68, 82, 86, 79 ; for ( i = 0 ; i <= 7 ; i = i + 1 ) show ( grades [ i ] ) ; printf ("\n\n") ; for ( i = 0 ; i <= 7 ; i = i + 1 ) printf ( "%d\n", grades [ i ] ) ; void show ( int p_grade ) p_grade = p_grade - 50 ; printf ( "%d\n", p_grade ) ; return ;
Now lets pass INDIVIDUAL ARRAY ELEMENT ADDRESSES to the function (aka "passing by address" but one address at a time) For arrays: passing to a function "by address" may mean a single element's address or the address of the array For arrays: when an address is passed, it's possible to access any array element and change its contents The program demonstrates that the original values are changed. #include <stdio.h> // 3.c void show ( int * ) ; int grades [ 8 ] = 76, 89, 92, 55, 68, 82, 86, 79 ; for ( i = 0 ; i <= 7 ; i = i + 1 ) show ( &grades [ i ] ) ; printf ("\n\n") ; for ( i = 0 ; i <= 7 ; i = i + 1 ) printf ( "%d\n", grades [ i ] ) ; void show ( int *p_grade ) *p_grade = *p_grade - 50 ; printf ( "%d\n", *p_grade ) ; return ;
Let's practice some pointer arithmetic. The next program demonstrates more pointer arithmetic. #include <stdio.h> // 5.c void show ( int * ) ; int grades [ 8 ] = 76, 89, 92, 55, 68, 82, 86, 79 ; printf("\n\nenter an index number between 0 and 7: ") ; scanf("%d", &i ); show ( &grades [ i ] ) ; printf ("\n\n") ; for ( i = 0 ; i <= 7 ; i = i + 1 ) printf ( "%d\n", grades [ i ] ) ; void show ( int *p_grade ) int input = 0 ; printf("\n\nenter an integer to be used to adjust the pointer: ") ; scanf("%d", &input ) ; *(p_grade + input) = 0 ; return ;
The next program print memory locations and the values found there. #include <stdio.h> // 6.c int grades [ 8 ] = 76, 89, 92, 55, 68, 82, 86, 79 ; for ( i = 0 ; i <= 7 ; i = i + 1 ) printf ( "Element number %d \t", i ) ; printf ( "address: %p \t", &grades[ i ] ) ; printf ( "integer value: %d \n", grades[ i ] ) ;
We don't need to use a for loop with arrays #include <stdio.h> // 7.c int grades [ 8 ] = 76, 89, 92, 55, 68, 82, 86, 79 ; while ( i <= 7 ) printf ( "Element number %d \t", i ) ; printf ( "address: %p \t", &grades[ i ] ) ; printf ( "integer value: %d \n", grades[ i ] ) ; i++ ;
Next, we access the array using a pointer and pointer notation. #include <stdio.h> // 8.c int grades [ 8 ] = 76, 89, 92, 55, 68, 82, 86, 79 ; int *iii = &grades[ 0 ] ; while ( i <= 7 ) printf ( "Element number %d \t", i ) ; printf ( "address: %p \t", (iii + i ) ) ; printf ( "integer value: %d \n", *( iii + i ) ) ; i++ ;
Next, we access the array using the array name and pointer notation. #include <stdio.h> // 9.c int grades [ 8 ] = 76, 89, 92, 55, 68, 82, 86, 79 ; while ( i <= 7 ) printf ( "Element number %d \t", i ) ; printf ( "address: %p \t", (grades + i ) ); printf ( "integer value: %d \n", *(grades + i ) ); i++ ;
We can pass a 1D array "by address/reference" using array notation #include <stdio.h> // 10.c void show ( int [ ], int ) ; int grades [ 8 ] = 76, 89, 92, 55, 68, 82, 86, 79 ; show ( grades, 7 ) ; printf ("\n\n") ; void show ( int p_grade[ ], int size ) while ( i <= size ) printf ( "Element number %d \t", i ) ; printf ( "address: %p \t", &p_grade[ i ] ); printf ( "integer value: %d \n", p_grade[ i ] ); i++ ; return ;
Or we can pass a 1D array "by address/reference" using pointer notation #include <stdio.h> // 11.c void show ( int *, int ) ; int grades [ 8 ] = 76, 89, 92, 55, 68, 82, 86, 79 ; show ( grades, 7 ) ; printf ("\n\n") ; void show ( int *p_grade, int size ) while ( i <= size ) printf ( "Element number %d \t", i ) ; printf ( "address: %p \t", ( p_grade + i ) ); printf ( "integer value: %d \n", *( p_grade + i ) ); i++ ; return ;
Teaching point: given a 1D integer array named grades, all of the following notations are equivalent. grades[ i ] *(grades + i ) *(i + grades ) Teaching point: given a 1D integer array named grades, all of the following notations are equivalent. &grade[ i ] (grade + i ) (i + grade ) #include <stdio.h> // 12.c int grades [ 8 ] = 76, 89, 92, 55, 68, 82, 86, 79 ; while ( i <= 7 ) printf ( "Element %d ", i ) ; printf ( "address: %p ", &grades[ i ] ); printf ( "%p ", ( grades + i ) ); printf ( "%p ", ( i + grades) ); printf ( " value: %d ", grades[ i ] ); printf ( "%d ", *( grades + i ) ); printf ( "%d \n", *( i + grades ) ); i++ ;
We can create/use 2D arrays. #include <stdio.h> // 13.c int student = 0, test = 0 ; int grades [ 6 ] [ 3 ] = 1, 4, 5, 13, 15, 19, 22, 23, 27, 35, 36, 38, 44, 45, 49, 50, 51, 52 ; for ( student = 0 ; student <= 5 ; student++ ) printf("\n") ; for ( test = 0 ; test <= 2 ; test++ ) printf("\t%3d ", grades[ student ] [ test ] ) ; printf("\n\n") ;
We can easily pass a 2D array to a function using array notation. Notice: do not generally provide the first dimension in the prototype and the formal parameters list to allow the function to be more general in nature. Notice: you must provide the second dimension in the prototype and the formal parameters list. #include <stdio.h> // 14.c void show ( int [ ] [ 3 ], int, int ) ; int grades [ 6 ] [ 3 ] = 1, 4, 5, 13, 15, 19, 22, 23, 27, 35, 36, 38, 44, 45, 49, 50, 51, 52 ; show (grades, 5, 3 ) ; printf("\n\n") ; void show (int p_grades [ ] [ 3 ], int num_students, int num_tests ) int row = 0, col = 0 ; for ( row = 0 ; row < num_students ; row++ ) printf("\n") ; for ( col = 0 ; col < num_tests ; col++ ) printf("\t%3d ", p_grades[ row ] [ col ] ) ; return ;