Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 103 Engineering Programming Chapter 41 C Pointers, Part 3 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.

Similar presentations


Presentation on theme: "ECE 103 Engineering Programming Chapter 41 C Pointers, Part 3 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material."— Presentation transcript:

1 ECE 103 Engineering Programming Chapter 41 C Pointers, Part 3 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed by Professor Phillip Wong @ PSU ECE

2 Syllabus Value Parameters Reference Parameters Passing 1-D Arrays to Functions Revisited Returning a Pointer from a Function

3 2 Pass by Value and Pass by Reference Generally, C parameters are passed by value With the exception of array parameters, which are passed by reference C++ also introduces explicit reference parameter passing via the & specifier; not part of C Consequences of "pass by value":  The function receives copies of the arguments, which are stored in local parameter variables  The function can change the parameter values, but it cannot change the values of the original arguments

4 3 Sometimes, the value of a passed argument needs to be changeable from inside the function. Example:  To return more than a single value from a function.  To swap the values of two arguments. How can this be done if the C language only supports pass by value? Answer: Pass the memory address of a variable instead of the value contained in that variable!

5 4 By using pointers as function parameters, the original arguments can be modified indirectly. To declare a parameter that is a pointer: return_dtype fun_name (…, dtype * param_name, …) { /* Function body */ } This allows C to simulate pass by reference.

6 5 While arguments can be changed via pointers, it is not required to do so. Passing just the pointer to a large data block may improve space and time efficiency. Example: You pass a data structure 1 MB in size to a function. If passed as a normal argument, then a duplicate 1 megabyte block has to be created. If a pointer to the data block is passed instead, then just a few bytes (the address) are transferred.

7 6 If a pointer is passed to a function, but the argument must not be changed, then use the const modifier in the parameter declaration. Example: void fun1 (char *s) { /* Can change what s points to */ } void fun2 (const char *s) { /* Cannot change what s points to */ }

8 7 Example: Function definition: void myfun (int a, float *b, float *c, float **d, float **e) { … } ParameterDatatype of parameterExpected argument a intinteger value b pointer to floatmemory address c pointer to floatmemory address d pointer to pointer to floataddress of a variable that contains another address e pointer to pointer to floataddress of a variable that contains another address

9 8 Invoking the function: void myfun (int a, float *b, float *c, float **d, float **e); int x = 3;/* Simple variable */ float y = 1.0;/* Simple variable */ float *p = &y;/* p points to y */ float **q = &p;/* q points to p */ myfun(x, &y, p, &p, q); /* Invoke */ Matching arguments to parameters: x  int a &y  float *b p  float *c &p  float **d q  float **e

10 9 Example: /* Non-pointer parameters */ #include #define PI 3.141592654 float circ (float R) { return 2*PI* R; } int main (void) { float radius = 0.75; float C; C = circ(radius); printf("%f\n", C); return 0; } /* Pointer parameters */ #include #define PI 3.141592654 float circ (float *R) { return 2*PI* (*R); } int main (void) { float radius = 0.75; float C; C = circ(&radius); printf("%f\n", C); return 0; } Expects a value Retrieve value Pass value to functionExpects an address Retrieve value stored at address Pass address to function

11 10 Example: /* Non-pointer parameters */ #include double mag (double x, double y) { return (sqrt(x*x + y*y)); } double angle (double x, double y) { return (atan2(y,x)); } int main (void) { double m, a; double u = 1.0, v = 1.0; m = mag(u, v); a = angle(u, v); return 0; } /* Pointer parameters */ #include void mang (double x, double y, double *m, double *a) { *m = sqrt(x*x + y*y); *a = atan2(y,x); } int main (void) { double m, a; double u = 1.0, v = 1.0; mang(u, v, &m, &a); return 0; }

12 11 Example: /* Try to swap values of a and b */ #include /* This will not work */ void swap (int x, int y) { int temp; temp = x; x = y; y = temp; } int main (void) { int a = 1, b = 3; printf("Pre : a=%d b=%d\n", a, b); swap(a, b); printf("Post: a=%d b=%d\n", a, b); return 0; } Actual Output: Pre : a=1 b=3 Post: a=1 b=3

13 12 Example: /* Try to swap values of a and b */ #include /* This will work */ void swap (int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; } int main (void) { int a = 1, b = 3; printf("Pre : a=%d b=%d\n", a, b); swap(&a, &b); printf("Post: a=%d b=%d\n", a, b); return 0; } Actual Output: Pre : a=1 b=3 Post: a=3 b=1

14 13 Passing 1-D Arrays to Functions Revisited If a function expects a 1-D array argument, the corresponding parameter can be declared using either array notation or pointer notation. Example: void fun1 (double A[]) { … } void fun2 (double *A) { … } Both forms work if the argument is a 1-D array. double v[5]; fun1(v); /* Remember that v by itself */ fun2(v); /* is the address of v[0]. */

15 14 Returning a Pointer from a Function A function can return a value. It can also return a pointer to a value. What does it mean to return a pointer? Answer: A memory address is returned. Declaration: return_datatype * function_name (…) { /* Function body */ }

16 15 Example: #include /* Capitalizes a string */ char * capstr (char * s) { char *p = s; while (*p) *p++ = toupper((int)*p); return s; /* Address */ } int main (void) { char s[] = "Hello"; printf("Pre : %s\n", s); printf("Post1: %s\n", capstr(s)); printf("Post2: %s\n", s); return 0; } Actual Output: Pre : Hello Post1: HELLO Post2: HELLO

17 16 Example: #include int * fun1 (int *s) { return s; } int * fun2 (int s) { return &s; } int main (void) { int x; printf("%p\n", &x); printf("%p\n", fun1(&x)); printf("%p\n", fun2(x)); return 0; } Actual Output: 0x22cd64 ← &x 0x22cd64 ← fun1(&x) 0x22cd40 ← fun2(x) fun1 Notes: The parameter *s is local to fun1. It contains the address that was passed to it (i.e., &x ), which is also the address that will be returned. fun2 Notes: The parameter s is local to fun2. When the address &s is returned, it is the address of the local parameter s, and not of the argument that was passed to fun2. When a function call is done, local variables defined in the function will no longer exist, and the memory addresses of those local variables become invalid.


Download ppt "ECE 103 Engineering Programming Chapter 41 C Pointers, Part 3 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material."

Similar presentations


Ads by Google