Functions: Pointer Arguments and Pass-by-Reference
For the execution of this program:
#include <stdio.h>
#include <stdlib.h>
void foo(int *b, int c, int *arr, int n) ;
void blah(int *r, int s);
int main() {
int x, y, *arr;
arr = (int *)malloc(sizeof(int)*5);
if(arr == NULL) {
exit(1); // should print out nice error msg first
}
x = 10;
y = 20;
printf("x = %d y = %d\n", x, y);
foo(&x, y, arr, 5);
printf("x = %d y = %d arr[0] = %d arr[3] = %d\n",
x, y, arr[0],arr[3]);
free(arr);
return 0;
}
void foo(int *b, int c, int *arr, int n) {
int i;
c = 2;
for(i=0; i<n; i++) {
arr[i] = i + c;
}
*arr = 13;
blah(b, c);
}
void blah(int *r, int s) {
*r = 3;
s = 4;
// STACK DRAWN HERE
}
The program's output is:
x = 10 y = 20
x = 3 y = 20 arr[0] = 13 arr[3] = 5
The array bucket values are:
13, 4, 5, 6, 7
The exectution stack drawn at the point show in function blah is:
STACK
|=================|
blah: | |
| --- --- |
| s | 4 | r | *---------------
| --- --- | |
| | |
|=================| |
foo: | | |
| --- --- | |
| n | 5 | i| 5 | | |
| --- --- | |
| --- --- | |
| c | 2 | b | *----------- |
| --- --- | | |
| --- | | |
| arr| *-----------| | |
| --- | | | | HEAP
|=================| | | | |====================================|
main: | | | | | | |
| ---- | |------------------->|--------------------| |
| arr | *------------------------------->| 13 | 3 | 4 | 5 | 6 | |
| ---- | | | | |--------------------| |
| | | | | |
| | | | |====================================|
| --- --- <---------- |
| y | 20| x| 3 |<--------------
| --- --- |
|=================|