190 likes | 211 Views
Learn about passing fixed-length arrays to functions, utilizing pointers, and understanding arrays of different lengths. Explore pointers to pointers and arrays of pointers, enhancing your C programming skills.
E N D
INC 161 , CPE 100Computer 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. • 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.
The size of the array can be either entered or omitted. • return_typefunction(type name[]) • { • } • Or • return_typefunction(type name[10]) • { • }
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
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.
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); }
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.
#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); }
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];
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:
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
If the address of i is 0x00E81E20, the value of p is 0x00E81E20 as shown below. Assume pp has address of 0x00E83060.
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;
Multiple strings can also be stored using 2D Array of char char day1[7][10] = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”};
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.
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
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.)
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;
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