0% found this document useful (0 votes)
21 views20 pages

Passing Arrays To Functions

Uploaded by

0987987972
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views20 pages

Passing Arrays To Functions

Uploaded by

0987987972
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Passing Arrays to Functions

Concept of Pass-by-Value & Pass-by-Reference


Outline
• Passing a 1-D array to a function

• An array is passed by reference

• Passing a 2-D array to a function

2
1. Passing a 1-D Array to a Function
• Why do we mention specifically about passing
arrays to functions?
• Short Answer: There are both syntactical and
conceptual issues.
– Let's start with syntax first

3
1. Passing a 1-D Array to a Function
1 void printArray(int array [], int arraySize) {
2 int i;
3 for (i = 0; i < arraySize; i++)
4 printf("%d ", array[i]);
5 printf("\n");
6 }
7
8 int main(void) {
9 int A[5] = { 1, 2, 3, 4, 5 }, B[10] = { 0 };
10
11 printArray( A, 5 ); // Output "1 2 3 4 5 "
12 printArray( B, 10 ); // Output "0 0 0 0 0 0 0 0 0 0 "
13 printArray( A, 3 ); // Output "1 2 3 "
14
15 return 0;
16 }
17

4
1. 1-D Arrays as Formal Parameters
1 void printArray(int array [], int arraySize) {
2 int i;
3 Needs
for (i = 0; i < arraySize; i++)a pair of [] after the
4 parameter name.
printf("%d ", array[i]);
5 printf("\n");
6 }
Number inside [], if any, is ignored.
7
8 int main(void) {
9 int A[5] = { 1, 2, 3, 4, 5 }, B[10] = { 0 };
10
11 printArray( A, 5 ); // Output "1 2 3 4 5 "
12 printArray( B, 10 ); // Output "0 0 0 0 0 0 0 0 0 0 "
13 printArray( A, 3 ); // Output "1 2 3 "
14
15 return 0;
16 }
17

5
1. 1-D Arrays as Actual Parameters
1 void printArray(int array [], int arraySize) {
2 int i;
3 for (i = 0; The
i < actual parameter
arraySize; i++)(argument) can be a 1-D array
4 of the
printf("%d ", same data type of ANY size.
array[i]);
5 printf("\n");
6 }
7
The array name, "A" or "B", already represents an
8 int main(void) {"array of int".
9 int A[5] = { 1, 2, 3, 4, 5 }, B[10] = { 0 };
10
11 printArray( A, 5 ); // Output "1 2 3 4 5 "
12 printArray( B, 10 ); // Output "0 0 0 0 0 0 0 0 0 0 "
13 printArray( A, 3 ); // Output "1 2 3 "
14
15 return 0;
16 }
17

6
1. Indicating Array Size via a Parameter
1 void printArray(int array [], int arraySize) {
2 int i;
3 for (i = 0; i < arraySize; i++)
4 The function is unaware of the size
printf("%d ", array[i]);
5 printf("\n"); of the actual parameter; the size is
6 } usually indicated using a separate
7 parameter.
8 int main(void) {
9 int A[5] = { 1, 2, 3, 4, 5 }, B[10] = { 0 };
10
11 printArray( A, 5 ); // Output "1 2 3 4 5 "
12 printArray( B, 10 ); // Output "0 0 0 0 0 0 0 0 0 0 "
13 printArray( A, 3 ); // Output "1 2 3 "
14
15 return 0;
16 }
17

7
2. A Curious Example
1 void clear(int A[], int size, int B) {
2 int i;
3 for (i = 0; i < size; i++)
4 A[i] = 0;
5 B = 0; Can you dry run the program and
6 } tell me your expected output?
7
8 int main(void) {
9 int C[3] = { 1, 2, 3 };
10 int D = 10;
11 clear(C, 3, D);
12 printf("%d %d %d %d\n", C[0], C[1], C[2], D);
13
14 return 0;
15 }
16

8
2. A Curious Example
1 void clear(int A[], int size, int B) {
2 int i;
3 for (i = 0; i < size; i++)
4 A[i] = 0;
5 B = 0; The actual output is:
6 } 0 0 0 10
7
8 int main(void) { Why would that be?
9 int C[3] = { 1, 2, 3 };
10 int D = 10;
11 clear(C, 3, D);
12 printf("%d %d %d %d\n", C[0], C[1], C[2], D);
13
14 return 0;
15 }
16

9
2. Array is passed to a function by reference
• When an array is passed to a function via a parameter,
the array is passed by reference (an ordinary variable is
passed by value).

• When a parameter is passed by reference, modifying


the formal parameter has the same effect on the actual
parameter.

• That is, an array parameter is SHARED between two


functions, as an actual parameter at the caller side AND
as a formal parameter at the callee.
10
2. When an int parameter is passed by value
1 void clear(int a) { a 5
2 a = 0;
3 } Value of b
4 is copied
5 int main(void) { to a
6 int b = 5;
7
b 5
8 clear(b);
9 // After line 8 is executed,
10 // b remains 5
11
12 return 0; When a parameter is passed by value, the value of the
13 } actual parameter is copied to the formal parameter;
14 they have their own storage space.
15
16 If you print the addresses of a and b, they are obviously
two different memory locations
11
2.When a parameter is passed by reference
1 void clear(int A[], int N) { When a parameter is passed by
2 int i;
3 for (i = 0; i < N; i++)
reference, the formal parameter
4 A[i] = 0; becomes an alias of the actual
5 } parameter during the function call.
6
7 int main(void) {
8 int B[5] = { 1, 2, 3, 4, 5 };
9
B 1 2 3 4 5
10 clear(B, 5);
11 // After line 10 is executed,
12 // all elements of B will
13 // become 0's
In this example, the formal
14 parameter A and the actual
15 return 0; parameter B refer to the same
16 } array during the function call.

12
2. When a parameter is passed by reference
1 void clear(int A[], int N) {
2 int i;
3 for (i = 0; i < N; i++)
If you print the addresses of A and
4 A[i] = 0;
5 printf("%p\n", &A[0]); B, you will find that they have the
6 } same address.
7
8 int main(void) {
9 int B[5] = { 1, 2, 3, 4, 5 };
10
11 clear(B, 5);
12 printf("%p\n", &B[0]);
13
14 return 0;
15 }
16

13
3. Pass-by-value vs. Pass-by-reference
• In most cases we would prefer pass-by-value for
our functions.
– In C, we generally do not expect to change our actual
parameters when we pass them into a function
• Pass-by-reference is most useful when you wish to
update the actual parameter passed in.
• At the end of the course you will get to learn how
to pass ordinary variables (non-arrays) by
reference (more accurately, to emulate).

14
3. Example: Using array to pass data from a callee to a caller
1 void readIntegers(int A[], int N) {
2 int i;
3 for (i = 0; i < N; i++)
4 scanf("%d", &A[i]);
5 }
6
7 int main(void) {
8 int B[100];
9
10 // Pass an array to the function to store 100 input.
11 readIntegers(B, 100);
12
13 return 0;
14 }
15
16

15
4. Passing a 2-D Array to a Function
1 void foo(int array[][64], int rows) {
2 // array should be treated in this function as a rowsx64 2D-array
3 ...
4 }
5
6 int main() {
7 int a1[24][64], a2[100][64], a3[10][2];
8
9 foo( a1, 24 ); // OK; process row 0-23
10
foo( a2, 100 ); // OK; process row 0-99
11
foo( a2, 10 ); // OK; process row 0-9
12
foo( a3, 10 ); // WRONG; different 2nd dimension
13
...
14
}
15
16
17

16
4. 2-D Arrays as Formal Parameters
1 void foo(int array[][64], int rows) {
2 // array should be treated in this function as a rowsx64 2D-array
3 ...
4 } The size of the 1st dimension, if
5 any, is ignored, but the size of 2nd
6 int main() {
7 int a1[24][64], dimension
a2[100][64], is required.
a3[10][2];
8
9 foo( a1, 24 ); // OK; process row 0-23
10
foo( a2, 100 ); // OK; process row 0-99
11
foo( a2, 10 ); // OK; process row 0-9
12
foo( a3, 10 ); // WRONG; different 2nd dimension
13
...
14
}
15
16
17

17
4. 2-D Arrays as Actual Parameters
1 void foo(int array[][64], int rows) {
2 // array should be treated in this function as a rowsx64 2D-array
3 ...
4 }
5
6 int main() {
7 int a1[24][64], a2[100][64], a3[10][2];
8
9 foo( a1, 24 ); // OK; process row 0-23
10
foo( a2, 100 ); // OK; process row 0-99
11
foo( a2, 10 ); // OK; process row 0-9
12
foo( a3, 10 ); // WRONG; different 2nd dimension
13
...
14 The actual parameter can be a 2-D array of the same
}
15
16 data type in which its second dimension must match.
17

18
4. Indicating the 1st Dimension Size via a Parameter
1 void foo(int array[][64], int rows) {
2 // array should be treated in this function as a rowsx64 2D-array
3 ...
4 } The function is unaware of the size of
5 the first dimension of the actual
6 int main() { parameter; the size is usually
7 int a1[24][64], a2[100][64], a3[10][2];
indicated using a separate parameter.
8
9 foo( a1, 24 ); // OK; process row 0-23
10
foo( a2, 100 ); // OK; process row 0-99
11
foo( a2, 10 ); // OK; process row 0-9
12
foo( a3, 10 ); // WRONG; different 2nd dimension
13
...
14
}
15
16
17

19
Reading Assignment
• C: How to Program, 8th ed, Deitel and Deitel
• Chapter 6 C Arrays
– Sections 6.7: Passing Arrays to Functions
– Sections 6.9: A Case Study
– Sections 6.11: Multidimensional Arrays

20

You might also like