OSU ENGR H192 - Lecture 15 - Arrays and Pointers

Unformatted text preview:

Arrays and PointersSlide 2Slide 3Slide 4Arrays and PointersRunning the ProgramSlide 7Arrays and PointersSlide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Lect 15 P. 1 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionArrays and PointersLecture 15Lect 15 P. 2 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionArrays and Pointers•So far, we have looked at pointer variables which were declared with statements like:FILE *fptr ;int *aptr ;•We have also used pointer constants when we sent the address of a variable to a function. We did this by placing an ampersand before the variable name. For instance, the address of variable a is &a, which is a pointer constant.Lect 15 P. 3 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionArrays and Pointers•The name of an array without a subscript (index number) is also a pointer constant. When the name is used without the subscript it references the address of element 0 of the array.int myarray[10]; /* Declare an array */int *myptr; /* Declare a pointer (not initialized)*/printf (“%d\n”, myarray); /* print address of myarray[0] */scanf (“%d”, myarray); /* get value from keyboard and store in myarray[0] */scanf (“%d”,&myarray[0]);/* same thing as above */Lect 15 P. 4 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionArrays and Pointersmyptr = &myarray[2]; /* Assign the address of the third element of myarray to myptr */printf(“%d”, *myptr); /* Print the value of what myptr is pointing to, i.e., the value of myarray[2] */•Note that the * in front of myptr de-references the pointer. That is, it says “use the value in the address that is pointed to.” •The following shows a small program and its output.Lect 15 P. 5 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education Coalition Arrays and Pointers/* Printing array values and addresses */#include <stdio.h>int main ( ){ int k; float a[4] = {1000, 2000, 3000, 4000}; printf ("k a &a[k] a[k]\n"); for (k=0; k<4; k++) printf ("%d %ld %ld %f\n", k, a, &a[k], a[k]); return 0 ;}Lect 15 P. 6 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionRunning the Programr1tel (~) millerm 52> a.outk a &a[k] a[k]0 2147462916 2147462916 1000.0000001 2147462916 2147462920 2000.0000002 2147462916 2147462924 3000.0000003 2147462916 2147462928 4000.000000r1tel (~) millerm 53>Lect 15 P. 7 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionArrays and Pointers•We have seen how to pass a pointer for a single valued variable to a function.•Sometimes we want to pass an entire array to a function. The name of an array without a subscript is a pointer constant that contains the address of element [ 0 ] of the array. Therefore, if we pass the array name with no subscript to a function, we are passing a pointer to that array.•The following program illustrates this.Lect 15 P. 8 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionArrays and Pointers /* Passing an array to a function */#include <stdio.h>#include <string.h>void myfunct (int , char [ ]);int main ( ){ char name[20] = "Michael J. Miller"; myfunct (20, name); return 0 ;}Lect 15 P. 9 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionArrays and Pointersvoid myfunct (int len , char text[ ]){ int k ; printf ("%d\n", strlen(text)) ; for (k = 0 ; k < len ; k++) printf ("%c", text[k]) ;}/*Program Output */17Michael J. MillerLect 15 P. 10 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionArrays and Pointers•Given the declaration int a[3] = { 1, 2, 3 } ; a is a pointer to (the address of) a[0] &a[0] is a pointer to a[0] a[0] is the value 1 ( *a is also the value 1) &a[1] is a pointer to a[1] a[1] is the value 2 &a[2] is a pointer to a[2] a[2] is the value 3 &a[0]+1 is a pointer to a[1] &a[0]+2 is a pointer to a[2]Lect 15 P. 11 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionArrays and PointersGiven the declaration int b[3][3] = {{1,3,5}, {7,9,11}, {13,15,17}};b is a pointer to b[0][0]b[0] is also a pointer to b[0][0]b[1] is a pointer to b[1][0] b[2] is a pointer to b[2][0] *b is a pointer to b[0] (special case)*b[1] is the value of b[1][0] (which is 7)*b[2] + 1 is the value of b[2][0] + 1 (which is 14)Lect 15 P. 12 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education Coalition/* Double subscript arrays and user-written functions */#include <stdio.h>void printarray (int [ ][7], int);int main( ) { int calendar[5][7]={{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}, {29,30,31,32,33,34,35}} ; printarray (calendar , 5);}Lect 15 P. 13 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education Coalitionvoid printarray (int cal[][7], int j){ int k, n ; for (k = 0 ; k < j ; k++) { for (n = 0 ; n < 7 ; n++)printf ("%3d ", cal[k][n]); printf ("\n"); }}Lect 15 P. 14 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education Coalition/* Double subscript arrays, user-written functions and pointer arithmetic */#include <stdio.h>void printarray (int * , int , int);int main ( ) { int calendar[5][7]= {{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}, {29,30,31,32,33,34,35}}; printarray (calendar[0] , 5 , 7);}Lect 15 P. 15 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education Coalitionvoid printarray (int *cal, int j, int m) { for (k = 0 ; k < j*m ; k += m) { for (n = 0 ; n < m ; n++)printf ("%3d ", *(cal+k+n)); printf ("\n"); }


View Full Document

OSU ENGR H192 - Lecture 15 - Arrays and Pointers

Documents in this Course
Strings

Strings

22 pages

Load more
Download Lecture 15 - Arrays and Pointers
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 15 - Arrays and Pointers and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 15 - Arrays and Pointers 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?