Functions and Structure
In this section, you are going to learn
What are the basic properties of a structure ?
What is Call by Value ?
What is Call by Reference ?
struct ABC {
type1 member1;
type2 member2;
type3 member3;
};
struct ABC x;
Consider a Structure
struct ABC {
int a;
int b;
int c;
};
struct ABC x;
Let us answer few basic questions about a Structure
How many structures can be stored in x
?
See Answer
Number of Structures = 1
How many bytes are there in this Structure ?
See Answer
Number of Bytes = 12
What is the sizeof the Structure ?
See Answer
sizeof(a) = Number of Bytes = 12
How many bits are there in this Structure ?
See Answer
Number of bits = sizeof(x) * 8 = 12 * 8 = 96 bits
Consider a structure
struct ABC {
int a;
int b;
int c;
};
struct ABC x;
Then below are the properties of a structure
Expression |
? |
---|---|
x |
|
&x |
|
*x |
NOT VALID |
sizeof(x) |
12 Bytes |
sizeof(&x) |
8 Bytes |
typeof(x) |
|
typeof(&x) |
|
Structure passed to a function
Step 1 : Define a structure
struct ABC {
int a;
int b;
int c;
};
struct ABC x;
Step 2 : Pass structure to a function
fun(x);
Step 3 : Define a function
fun
void fun(struct ABC x)
{
}
Step 4 : Change structure inside function
fun
void fun(struct ABC x)
{
x.a = 10;
x.b = 20;
x.c = 30;
}
See full program below
#include <stdio.h>
struct ABC {
int a;
int b;
int c;
};
void fun(struct ABC x)
{
x.a = 10;
x.b = 20;
x.c = 30;
}
int main(void)
{
struct ABC x = {.a = 1, .b = 2, .c = 3};
printf("----- Before Call By Value -----\n");
printf("x.a = %d, x.b = %d, x.c = %d\n", x.a, x.b, x.c);
fun(x);
printf("----- Before Call By Value -----\n");
printf("x.a = %d, x.b = %d, x.c = %d\n", x.a, x.b, x.c);
return 0;
}
Output is as below
----- Before Call By Value -----
x.a = 1, x.b = 2, x.c = 3
----- Before Call By Value -----
x.a = 1, x.b = 2, x.c = 3
Can you guess what is happening ?
After function fun
is called,
There are two stack frames
Stack frame of
main
Stack frame of
fun
variable
c
is created on stack frame ofmain
variable
c
is created on stack frame offun
Even though name of variable
c
is same inmain
andfun
, they are two different variables in memoryHence changing value of
c
inside functionfun
does not change the value ofc
insidemain
Address of Structure passed to a function
Step 1 : Define a structure
struct ABC {
int a;
int b;
int c;
};
struct ABC x;
Step 2 : Pass address of structure to a function
fun(&x);
Step 3 : Define a function
fun
void fun(struct ABC *p)
{
}
Step 4 : Change structure inside function
fun
void fun(struct ABC *p)
{
p->a = 10;
p->b = 20;
p->c = 30;
}
See full program below
#include <stdio.h>
struct ABC {
int a;
int b;
int c;
};
void fun(struct ABC *p)
{
p->a = 10;
p->b = 20;
p->c = 30;
}
int main(void)
{
struct ABC x = {.a = 1, .b = 2, .c = 3};
printf("----- Before Call By Reference -----\n");
printf("x.a = %d, x.b = %d, x.c = %d\n", x.a, x.b, x.c);
fun(&x);
printf("----- After Call By Reference -----\n");
printf("x.a = %d, x.b = %d, x.c = %d\n", x.a, x.b, x.c);
return 0;
}
Output is as below
----- Before Call By Reference -----
x.a = 1, x.b = 2, x.c = 3
----- After Call By Reference -----
x.a = 10, x.b = 20, x.c = 30
Can you guess what is happening ?
Let us solve it with equations method !
Rule 1 : Base Rule
p = &x
RHS is actual parameter. In this case
&x
is actual parameterLHS is formal parameter. In this case
p
is formal parameter
Rule 2 : Move
&
from RHS to LHS. This becomes*
on LHS
*p = x
Rule 3 : Changing
*p
also changesx
. Because we proved*p
is equal tox
Function Call |
Function Definition |
Observations |
---|---|---|
fun(x) |
void fun(struct ABC x) { } |
Changing |
fun(&x) |
void fun(struct ABC *p) { } |
Changing |
Other topics of structure and functions
Current Module
Previous Module
Next Module
Other Modules