Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 103 Engineering Programming Chapter 32 Array Parameters

Similar presentations


Presentation on theme: "ECE 103 Engineering Programming Chapter 32 Array Parameters"— Presentation transcript:

1 ECE 103 Engineering Programming Chapter 32 Array Parameters
Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed by Professor Phillip PSU ECE

2 Syllabus 1D Array Parameters Multi-D Array Parameters Examples

3 Passing Arrays to Functions (1-D)
Parameter list declaration: dtype fun_name (dtype array_name[size]) { /* Function body */ } In the parameter list, specifying the 1-D array’s size is optional (the brackets are still required). When passing an array to a function, use just the array name as the argument to the function. Do not pass the brackets! (pass M, not M[]) 2

4 Example: #include <stdio.h> #define SIZE 3 int myfun (int c, int P[SIZE], int Q[]); int main (void) { int M[SIZE] = {12, 32, -5}; int Q[] = {1, 3, 4}; printf("%d\n", myfun(127, M, Q)); return 0; } int myfun (int c, int P[SIZE], int Q[]) { return P[0]*Q[2] + P[1]*Q[1] + P[2]*Q[0] + c; } 3

5 Only the memory address of the array is passed to the function.
Pass-by-value: A passed array address that is stored in a function parameter is only a local copy. The address value cannot be permanently altered. However, the contents of the array at that address can be permanently changed! If the values of a passed array should never be changeable, then use a const modifier. If a function needs to know the number of elements in a passed array, that number must also be passed as a separate argument. 4

6 Example: #include <stdio.h> #define SIZE 100 void printarray (int P[], int NumValidElements); int main (void) { int M[SIZE], value, k = 0; while (k < SIZE) { scanf("%d", &value); if (value < 0) break; M[k++] = value; } printarray(M, k); return 0; } void printarray (int P[], int NumValidElements) { int k; for (k = 0; k < NumValidElements; k++) printf("%d ", P[k]); printf("\n"); 5

7 Example: #include <stdio.h> #define SIZE 10
/* Declare P to be a normal changeable array */ /* Declare Q to be a constant unchangeable array */ void fun (int P[], const int Q[], int ArySize); int main (void) { int M[SIZE] = {0}, N[SIZE] = {0}; fun(M, N, SIZE); return 0; } void fun (int P[], const int Q[], int ArySize) { P[0] = 99; /* This is allowed */ Q[0] = 99; /* ERROR: Compiler will complain */ } 6

8 Example: Actual output: y=99 : x=1 2 y=99 : x=1024 4096
#include <stdio.h> void fun(int y, int x[]) { y = 42; x[0] = 1024; x[1] = 4096; } int main (void) int y = 99; int x[] = {1, 2}; printf("y=%d : x=%d %d\n", y, x[0], x[1]); fun(y, x); return 0; Actual output: y=99 : x=1 2 y=99 : x= 7

9 Passing Arrays to Functions (Multi-Dimensional)
Parameter list declaration: dtype fun_name (dtype array_name[size1][size2]…[sizeN]) { /* Function body */ } Specifying size1 is optional (the brackets are still required). The remaining sizes must be specified. When passing an array to a function, use just the array name as the argument to the function. Do not pass the brackets! (pass M, not M[][]) 8

10 Example: #include <stdio.h>
void myfun (int P[7][3][2], int Q[][2][3]); int main (void) { int M[7][3][2] = {{{0}}}; int N[2][2][3] = {{{0}}}; myfun(M, N); return 0; } void myfun (int P[7][3][2], int Q[][2][3]) /* Function body */ 9


Download ppt "ECE 103 Engineering Programming Chapter 32 Array Parameters"

Similar presentations


Ads by Google