90 likes | 213 Views
CS 108 Computing Fundamentals Notes for Thursday, March 27, 2014. GHP #8 is posted due Sunday (March 30) by midnight GHP #9 will be posted tomorrow or Saturday due Friday (April 4) by midnight GHP #10 will be posted on Tuesday (April 1) Due Tuesday. April 8 by midnight. Class Notes #1.
E N D
CS 108 Computing FundamentalsNotes for Thursday, March 27, 2014
GHP #8 is posted due Sunday (March 30) by midnight GHP #9 will be posted tomorrow or Saturday due Friday (April 4) by midnight GHP #10 will be posted on Tuesday (April 1) Due Tuesday. April 8 by midnight Class Notes #1
Hope to have Exam #2 graded and back to you on Tuesday, April 1 Last day to withdraw: Monday, April 7 http://www.sunyit.edu/calendars_events/cal_spr14 Class Notes #2
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
Given: int our_array[20] ; int *slick ; int index = 0 ; slick = our_array ; our_array[index] is the same as: *(our_array + index)(or *our_array (if index = 0) ) *(slick + index) (or *slick (if index = 0) ) &our_array[index]is the same as: our_array + index (or our_array (if index = 0) ) slick + index ( or slick (if index = 0) ) Notice that the name of the array and the name of the pointer are treated exactly alike. Pointers and Arrays
C allows us to perform pointer arithmetic: slick = slick + 5; is the same as: slick = &(our_array[5]); Relational operators ( <, >, <=, >=, = = , != ) can be used on pointers Pointer Arithmetic
#include <stdio.h> //My file 22.c #define size 18 int main ( void ) { int golf_score[size] = {2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36}; int *hole_ptr = golf_score; printf("\n\nThe address of golf_score[0] is %p ", &golf_score[0] ); printf("\n\nThe address of the array golf_score is %p \n\n\n", golf_score ); printf("\n\nThe address of golf_score[0] using hole_ptr %p ", hole_ptr ); printf("\n\nThe address of the array golf_score is %p \n\n\n", golf_score ); printf("\n\nThe value of golf_score[3] using the array notation is %d ", golf_score[3] ); printf("\n\nThe value of golf_score[3] using hole_ptr and pointer "); printf("\nnotation is %d ", *(hole_ptr + 3) ); printf("\n\nThe value of golf_score[3] using golf_score as a ") ; printf("\npointer and pointer notation is %d ", *(golf_score + 3)); printf("\n\nThe value of golf_score[3] using hole_ptr and array "); printf("\nnotation is %d \n\n\n", hole_ptr[3] ); return (0); } Pointer Arithmetic
#include <stdio.h> // My file 33.c float calc_average (int * , int); // 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 first value in demo is: %d ", *demo ); // demo[0] printf("\n\nThe last value in demo is: %d ", *(demo + 4) ); // 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) // 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) ; // p_array [ index ] } return ( (float) total / p_array_size) ; } Array Notation vs. Pointer Notation
http://web.cs.sunyit.edu/~urbanc/cs_108_mar_27a.html Array Boot Camp