1 / 16

CS 108 Computing Fundamentals Notes for Thursday, October 23, 2014

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.

Download Presentation

CS 108 Computing Fundamentals Notes for Thursday, October 23, 2014

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 108 Computing FundamentalsNotes for Thursday, October 23, 2014

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. #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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. http://web.cs.sunyit.edu/~urbanc/cs_108_oct_23a.html Pointers and Arrays

More Related