Structs
Pass Struct
Passing a struct by value gives a function its own copy of the fields.
Pass Struct
pass_struct.c
#include <stdio.h>
struct Box {
int width;
int height;
};
int area(struct Box box) {
return box.width * box.height;
}
int main(void) {
int width = ;
struct Box box = {width, 5};
int result = area(box);
printf("area=%d\n", result);
return 0;
}
#include <stdio.h>
struct Box {
int width;
int height;
};
int area(struct Box box) {
return box.width * box.height;
}
int main(void) {
int width = ;
struct Box box = {width, 5};
int result = area(box);
printf("area=%d\n", result);
return 0;
}
#include <stdio.h>
struct Box {
int width;
int height;
};
int area(struct Box box) {
return box.width * box.height;
}
int main(void) {
int width = ;
struct Box box = {width, 5};
int result = area(box);
printf("area=%d\n", result);
return 0;
}
by value
The function receives a copy of the struct argument.
compute from fields
The function can read fields from its copy to compute a result.