I am learning about structures in C. when we pass a structure as an argument to another function, what exactly are we doing? are we copying all the member values in another function? When we pass an array as argument, we are passing the first address of the array - array[0]. things don't seem so simple with structure. My guess is that we are neither copying any value nor passing any address. we are telling compiler to be ready to use, as and when required, the addresses of the members of the structure being passed. I am not sure though.. any help is appreciated.
4 Answers
Structres can be passed both by value and reference. If there is a big structure its advisable to pass by reference else huge amount of stack space may be consumed as a copy of the passed structure will be made in the called function Read this to know more about structure passing
Are there any downsides to passing structs by value in C, rather than passing a pointer?

- 1
- 1

- 27,404
- 12
- 99
- 125
-
So Structures cannot be made globally available to all the functions in the source code file, unlike arrays? Am I right to say so? they need to be passed every time other functions needs them? – KawaiKx May 03 '12 at 15:07
-
1@Saurabh No. There can be global structures just like arrays which can accesses across all functions. Just because they can be passed by value doesn't mean that is the only way of accessing them across functions. – Pavan Manjunath May 03 '12 at 17:08
It depends on how the struct
is passed:
- if passed by value a copy is made and changes made inside the function to the
struct
are not visible to the caller - if passed by reference a copy is not made and changes made inside the function to the
struct
are visible to the caller
For example:
#include <stdio.h>
struct X {
int value;
};
void pass_by_value(struct X a_x)
{
a_x.value = 10;
printf("by_value(address=%p, value=%d)\n", &a_x, a_x.value);
}
void pass_by_ref(struct X* a_x)
{
a_x->value = 20;
printf("by_ref(address=%p, value=%d)\n", a_x, a_x->value);
}
int main()
{
struct X my_x = { 1 };
printf("my_x(address=%p, value=%d)\n\n", &my_x, my_x.value);
pass_by_value(my_x);
printf("my_x(address=%p, value=%d)\n\n", &my_x, my_x.value);
pass_by_ref(&my_x);
printf("my_x(address=%p, value=%d)\n\n", &my_x, my_x.value);
return 0;
}
Output:
my_x(address=0013FF74, value=1) by_value(address=0013FF78, value=10) my_x(address=0013FF74, value=1) by_ref(address=0013FF74, value=20) my_x(address=0013FF74, value=20)

- 120,187
- 20
- 207
- 252
-
Here you have used only one member in the structure. suppose there are many members in the structure. in such case, what would be passed by ' pass_by_value(my_x)' ? and what would print by ' &my_x' ? thanks for your patience.. – KawaiKx May 03 '12 at 14:20
-
-
#include <stdio.h>
struct foo {
int a;
}
void by_value(struct foo x) {
x.a = 10;
}
void by_reference(struct foo* x) {
x->a = 10;
}
int main(int argc, char* argv[]) {
struct foo v;
v.a = 5;
by_value(v);
printf("%d\n", v.a);
by_reference(&v);
printf("%d\n", v.a);
return 0;
}
This should answer your question if you compile it and run it.

- 1,961
- 11
- 11
-
1@Saurabh If you pass it by value, everything in it will be copied. The number of members does not matter. But keep in mind that the copy is shallow. This means that if the struct contains pointers, the pointer value will be copied, not what it is pointing to. – HonkyTonk May 03 '12 at 15:03
-
like arrays, can structures be declared globally? or are structures function specific? – KawaiKx May 03 '12 at 15:13
-
1@Saurabh A structure is a type, nothing more. If you want to declare a variable with a struct type globally, it works the same way as declaring any other type globally. But, if you declare it globally to not have to send a reference to a struct when you do a function call, you are solving the wrong problem. Always keep the number of global variables to a minimum. – HonkyTonk May 03 '12 at 16:22
Before we call a function, the compiler pushes the parameters into the stack first, and then it invokes the function. So, the function can get the parameter from the stack.
When passed by address , the address is passed to the function. And the compiler will also process the parameter memory, which is called "Memory alignment" or something like that, to improve performence. So, in your function, you can easily covert that block of memory to a structure.
When passed by value, the compiler copes the parameter and pass the coped parameter address to your function.
By the way, in one process, the memory address is the same. Any function can use it.

- 1,822
- 15
- 29