1 / 19

INC 161 , CPE 100 Computer Programming

INC 161 , CPE 100 Computer Programming. Lecture 12 Pointer (part 2). Pass Arrays of Fixed Length to Function. Pass one dimensional array When an array is passed to a function, what is actually passed is the address of the first element of the array.

geraldinem
Télécharger la présentation

INC 161 , CPE 100 Computer Programming

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. INC 161 , CPE 100Computer Programming Lecture 12 Pointer(part 2)

  2. Pass Arrays of Fixed Length to Function • Pass one dimensional array • When an array is passed to a function, what is actually passed is the address of the first element of the array. • In calling function, specify array name without brackets type name[10]; …… function(name); Note that name is equivalent to a pointer • The definition of called function return_type function(type name[]){ } The array_name in the function definition is not necessary the same as in the function call.

  3. The size of the array can be either entered or omitted. • return_typefunction(type name[]) • { • } • Or • return_typefunction(type name[10]) • { • }

  4. Example 1:Function that add two arrays Output: #include <stdio.h> void oneDadd(double dd1[], double dd2[5]) { int i; for(i = 0; i <5; i++) { dd1[i] += dd2[i]; } } main() { double d1[5] = {1, 2, 3, 4, 5}; double d2[5] = {1, 2, 3, 4, 5}; int i; oneDadd(d1, d2); for(i = 0; i < 5; i++) { printf("%f ", d1[i]); } printf(“\n"); } 2.00 4.00 6.00 8.00 10.00

  5. Pass two-dimensional array • In calling function, specify array name without brackets int a1[2][3], a2[2][3]; …… function(a1, a2); • The definition of called function return_type function (int a1[2][3], int a2[][3]){ } The name in the function definition is not necessary the same as in the function call.

  6. Example 2:Function that add two 2Darrays Input: d2 = 1.00 2.00 3.00 4.00 5.00 6.00 d3 = 1.00 2.00 3.00 4.00 5.00 6.00 Output: d1 = 2.00 4.00 6.00 8.00 10.00 12.00 void twoDadd(double dd1[][3], double dd2[][3], double dd3[2][3]) { int i, j; for(i = 0; i < 2; i++) { for(j = 0; j < 3; j++) { dd1[i][j] = dd2[i][j]+dd3[i][j]; } } } main() { double d1[2][3], d2[2][3]={{1,2,3},{4,5,6}}, d3[2][3]={{1,2,3},{4,5,6}}; twoDadd(d1, d2, d3); }

  7. Using Pointers to Pass One-Dimensional Arrays to a Function • When an array is passed to a function, what is actually passed is the address of the first element of the array. • Array name = address of the first element of the array • This is why pointers can be used to pass one-dimensional arrays to functions.

  8. #include <stdio.h> double func(double aa[]) { double sum = 0; int i; for(i=0; i<5; i++) { sum += aa[i]; } return sum; } main() { double a[5] = {1, 2, 3, 4, 5}; double sum; sum = func(a); printf("sum = %f\n", sum); } #include <stdio.h> double func(double *aa) { double sum = 0; int i; for(i=0; i<5; i++) { sum += *(aa+i); } return sum; } main() { double a[5] = {1, 2, 3, 4, 5}; double sum; sum = func(a); printf("sum = %f\n", sum); }

  9. The function definition double func(double *aa) can be replaced by double func(double aa[]) • The statement sum += *(aa+i); can be replaced by sum += aa[i];

  10. Pointers to Pointers • A pointer to a pointer is defined as a variable which contains the address of another pointer. • Consider the following code: int i; // i is an integer i = 10; int *p = &i; // p is a pointer pointes to i int **pp = &p; // pp pointes to p Here, **p refers to memory address of pointer p which refers to the memory address of variable i. It can be illustrated by the following figure:

  11. Example: > int i, *p > p = &i // assign address of i to p 00E81E20 > *p = 10 10 > printf(“i = %d\n”, i); i = 10 > int **pp > pp = &p // assign address of p to pp 00E82040 > printf(“**pp = %d\n”, **pp) **pp = 10 **pp == *(*pp) == *p == i

  12. If the address of i is 0x00E81E20, the value of p is 0x00E81E20 as shown below. Assume pp has address of 0x00E83060.

  13. Array of Pointers • Since pointers are also variables, arrays of pointers are supported. • Array of pointers can store indexes to multiple strings • Below is an example of how arrays of pointers can be declared and used: char *s1 = “abc”; char *s2 = “123”; char *p[3]; //declares an array of 3 pointers to char; p[0] = s1; p[1] = s2; p[2] = NULL;

  14. Multiple strings can also be stored using 2D Array of char char day1[7][10] = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”};

  15. Array of pointers to char char *day2[7] = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”}; • The expression day2[1][3] has the value of ‘d’ of “Monday”. The advantage of using day2, the array of pointers, is that each pointer can point to arrays with different length rather than the fixed length of 10 bytes.

  16. Use NULL to indicate the end of array of strings. Example: A list of names terminated by NULL. char *p[] = {“John", “Doe", “Peter”, NULL}; int i; for(i=0; p[i]!=NULL; i++) { printf(“p[i] = %s\n”, p[i]); } Output: John Doe Peter

  17. The following example demonstrates how an array of pointers can be used to eliminate complicated storage management and overheads of moving lines. Example: char *p[3] = {"ABC", "HIJKL", "EF"}; char *tmp; ... tmp = p[1]; p[1] = p[2]; p[2] = tmp; (Before swapping texts.) (After swapping texts.)

  18. Pointer to Functions • A function pointer is a variable containing the address of the function. • A function’s address is the entry point of the function. So, a function’s pointer can be used to call a function. • A function pointer can be assigned, placed in arrays, passed to functions, returned by functions, etc. • Below are some declarations of function pointers. void (*pf1) (void); int (*pf2) (); int (*pf3) (double f); • Similar to an array name, a function name also represents for the address of a function. However, the address operator ‘&’ is ignored. For example, the statement pf = &fun; is treated as pf = fun;

  19. Output: Example: Call function by using function’s pointer #include <stdio.h> int fun(double f) { printf("f = %f\n", f); return 0; } main() { int (*pf)(double f); fun(10.0); pf = fun; // fun = address of the function pf(20.0); // call function fun by calling pf } f = 10.000000 f = 20.000000

More Related