Originally Posted by
tabstop
That's right, but the point being you can return the newly-whatever'ed struct back to the main (so not pass by reference, but returning a value).
I still don't quite understand. I made this program:
Code:
#include <stdio.h>
#include <stdlib.h>
struct test
{
int var;
};
void add(struct test a, int num) {
a.var += num;
}
void addptr(struct test* a, int num) {
a->var += num;
}
int main()
{
struct test* a = malloc(sizeof(struct test));
a->var = 0;
printf("Initial var : %d\n", a->var);
add(*a, 1);
printf("Add 1 (copy): %d\n", a->var);
addptr(a, 1);
printf("Add 1 (ptr): %d\n", a->var);
free(a);
return 0;
}
And it prints out:
Code:
Initial var : 0
Add 1 (copy): 0
Add 1 (ptr): 1