160 likes | 270 Views
CS 108 Computing Fundamentals Notes for Thursday, October 23, 2014. GHP #8 and GHP #9 combined Worth 60 points Due by 2 PM on Tuesday, October 28 Exam #2 will be returned on Tuesday, October 28. Class Notes.
E N D
CS 108 Computing FundamentalsNotes for Thursday, October 23, 2014
GHP #8 and GHP #9 combined Worth 60 points Due by 2 PM on Tuesday, October 28 Exam #2 will be returned on Tuesday, October 28 Class Notes
You can use individual array elements just as you would any other variables in a function call PCF prototype example: char score_convert ( int ); PCF call example: score = score_convert ( test_scores[7] ); PCF declaration header example: char score_convert ( int score_input ) Arrays and Functions
You may wish to pass an entire array as a parameter You should always pass the size of the array, too PCF prototype example: void calc_stats ( int [ ] , int ) ; PCF call example:calc_stats( test_scores , students ) ; Note: test_scores is an array PCF declaration header example: void calc_stats ( int score[ ] , int num_students ) Arrays and Functions Notice that the [ ] are missing
Inside the PCF elements are treated the same as they would be in the main function:void calc_stats (int score[ ], num_students){ int index; float average = 0.0; for (index = 0 ; index < num_students ; index = index + 1) { total = total + score[index]); } printf("The average score is: %f. ", (float) total / num_students); return ; } Arrays and Functions
A function cannotreturn an array (it can return a pointer... later!!) A whole array is passed to a function by address Individual array elements are passed to a function by value Unless we use "pointer notation"… more on this later If you want to “protect” the values of a whole passed array, you can use the const operator in the PCF’s prototype and header: PCF prototype example: void calc_average ( const int [ ] , int ); PCF declaration header example: void calc_average ( const int score[ ] , int num_students) Arrays and Functions
#include <stdio.h> // My file 44.c float calc_average (int [ ] , int); int main (void) { int demo [ 5 ] = {200 , 50 , 60 , 30 , 4 } ; int array_size = 5; float average = 0.0; average = calc_average ( demo , array_size ) ; printf("\n\nThe zeroth value in demo is: %d ", demo [ 0 ] ); printf("\n\nThe last value in demo is: %d ", demo [ 4 ] ); printf("\n\nThe average value in demo is: %.2f \n\n\n ", average); return 0; } float calc_average (int p_array [ ] , int p_array_size ) { int index = 0, total = 0; for ( index = 0 ; index < p_array_size ; index = index + 1) { total = total + p_array [ index ] ; } return ( (float) total / p_array_size) ; } Array Notation vs. Pointer Notation
Variables have four characteristics: name data type value location (an address) A pointer is a variable that stores the memory address of another variable (points to it) Variable Addresses
Assume the following variable declarations: int mullet_id = 12; (address might be 0xbfbff76c) float mullet_weight = 4.1; (address might be 1xbaaf6c6) char alvin = 'Q'; (address might be 0ababdda6a) Each variable will have a unique address assigned by the compiler/OS The unique address will stay constant for the duration of the program run but will probably change each time then program is run (depends on compiler, linker, and OS) Variable Addresses
Use the %p format specifier to display the address of a variable # include <stdio.h> //1.c int main ( ) { int mullet_id = 12; printf("mullet_id value= %d, address= %p", mullet_id, &mullet_id); printf("\n\n"); return 0; } Variable Addresses
Very often we don’t care about the address of a variable However, being able to refer to a variable by address is often very useful Pointer variables are declared using (*): float *engine_temp; float radiator_temp = 0.0; int *tickets_available; int arena_capacity = 6,345; Pointer variables store the addresses of other variables (must use the address of operator) engine_temp = &radiator_temp; tickets_available = &arena_capacity; Pointers
int *tickets_available ; int arena_capacity = 6345 ; tickets_available is a pointer to a variable that is of type integer Pointers • tickets_available = &arena_capacity; • tickets_available arena_capacity • 0xbabdda3a 0xbfbff76c 0xbfbff76c 6,345
Given: int our_array[20]; The address of our_array[0] is at a specific location (0xbfbff76c, for example)… the other elements in our_arrayare held in consecutive memory locations int *slick; slick = our_array; //slick now holds address of a[0]… // notice there is no address operator slick = &our_array[0]; //same as above Pointer notation can be used instead of array notation (subscripts) to access array elements. Pointers and Arrays same
Major Teaching/Learning Point: Array names are Pointer Constants An array name stores the address of the 0th element of an array… the array name stores an address value and not a int, float, char, etc… Pointers and Arrays
http://web.cs.sunyit.edu/~urbanc/cs_108_oct_23a.html Pointers and Arrays