Search . . .
COLLEGE OF COMPUTING AND INFORMATION
SCIENCES
CIS 102
COMPUTER
PROGRAMMING II
LECTURE 4: PASSING ARRAY TO A
FUNCTION
STEM
Winchester R. Gerez Workshop
Faculty, Department of Computer Science
VISIT OUR WEBSITE
[Link]
m
COLLEGE OF COMPUTING AND INFORMATION SCIENCES Department of Computer Science
COLLEGE OF COMPUTING AND INFORMATION
SCIENCES
ARRAYS ARE PASSED BY
REFERENCE
What Actually Happens?
void processArray(int
arr[], int size);
You're passing a pointer to the first element (arr[0]), which
means:
Inside the function:
• arr behaves like a pointer: int* arr
• You can use arr[i] or pointer arithmetic like *(arr + i)
Outside the function:
• Any changes inside the function will reflect on the original
array
COLLEGE OF COMPUTING AND INFORMATION SCIENCES Department of Computer Science
COLLEGE OF COMPUTING AND INFORMATION
SCIENCES
ARRAYS ARE PASSED BY
REFERENCE
Why?
void processArray(int
arr[], int size);
Arrays in C++ cannot be passed by value because:
• Copying entire arrays is inefficient (high memory cost)
• The compiler implicitly converts the array into a pointer
when passing to a function
This is called "decaying":
int arr[] 👉 becomes 👉 int* arr
COLLEGE OF COMPUTING AND INFORMATION SCIENCES Department of Computer Science
COLLEGE OF COMPUTING AND INFORMATION
SCIENCES
ARRAYS ARE PASSED BY
REFERENCE
COLLEGE OF COMPUTING AND INFORMATION SCIENCES Department of Computer Science
COLLEGE OF COMPUTING AND INFORMATION
SCIENCES
ARRAYS ARE PASSED BY
REFERENCE
What Happens During
This Call?
When processArray(numbers, 3);
is
Step 1: Declareexecuted:
the Array
• Memory is allocated for 3 integers in a contiguous
memory block.
• The variable numbers stores the address of the first
element (&numbers[0]).
• numbers points to address of the first element.
COLLEGE OF COMPUTING AND INFORMATION SCIENCES Department of Computer Science
COLLEGE OF COMPUTING AND INFORMATION
SCIENCES
ARRAYS ARE PASSED BY
REFERENCE
What Happens During
This Call?
When processArray(numbers, 3);
Step 2: is executed:
Function Call
You are passing:
• numbers which is interpreted as &numbers[0] →
type: int*
• 3 -> the size of the array
COLLEGE OF COMPUTING AND INFORMATION SCIENCES Department of Computer Science
COLLEGE OF COMPUTING AND INFORMATION
SCIENCES
ARRAYS ARE PASSED BY
REFERENCE
What Happens During
WhenThis Call?
processArray(numbers, 3);
is executed:
Step 3: Inside the
Function
arr is a pointer to the same memory location as numbers in main().
So:
• arr and numbers point to the same data
• If you modify arr[i], it also modifies numbers[i] in main()
COLLEGE OF COMPUTING AND INFORMATION SCIENCES Department of Computer Science
COLLEGE OF COMPUTING AND INFORMATION
SCIENCES
ARRAYS ARE PASSED BY
REFERENCE
How the Connection Looks
Visually
They point to the same memory
COLLEGE OF COMPUTING AND INFORMATION SCIENCES Department of Computer Science
COLLEGE OF COMPUTING AND INFORMATION
SCIENCES
ARRAYS ARE PASSED BY
REFERENCE
How the Connection Looks
Visually
Because they share
memory, any change
made in
processArray()
reflects in main().
COLLEGE OF COMPUTING AND INFORMATION SCIENCES Department of Computer Science
Get in
Touch
With
Send usUs
a message
or visit us
City of Batac, Ilocos Norte,
Philippines
(63) 77-600-0459
op@[Link]
Follow us for
updates
[Link]/MMSUofficial
[Link]