After the example code is a drawing of the stack right before the return from kooky.
The program:
#include <stdio.h>
#include <string.h>
// a function prototype:
void kooky(int a[], int n);
/**********************************************************
* main function:
*/
int main() {
int x, i;
int arr[5];
for(i=0; i < 5; i++) {
arr[i] = i;
}
for(i=0; i < 5; i++) {
printf("arr[%d] = %d\n", i, arr[i]); // bucket values: 0 1 2 3 4
}
kooky(arr, 5); // array argument: passes value of base address of array
for(i=0; i < 5; i++) {
printf("arr[%d] = %d\n", i, arr[i]); // bucket values: 0 1 4 9 16
}
}
/**********************************************************
* kooky function definition: what does this function do?
* a: an array of int values
* n: size of the array
*/
void kooky(int a[], int n) {
int i;
for(i=0; i < n; i++) {
a[i] = a[i]*a[i];
}
//**** THE STACK IS DRAWN RIGHT BEFORE THE return STATMENT IS EXECUTED
return;
}
/**********************************************************/
The stack right before kooky returns:
|===========================================|
kooky: | ------- |
| i | 5 | |
| ------- |
| ------- |
| n | 5 | |
| ------- |
| --------- |
| a | * | |
| ----|---- |
|============|==============================|
main: | | |
| | |
| | |
| | |
| \/ |
| ----------------------- |
| arr | 0 | 1 | 4 | 9 | 16 | |
| ----------------------- |
| ------- |
| x | 10 | |
| ------- |
| ------- |
| i | 5 | |
| ------- |
|===========================================|
STACK