Can anybody tell me the advantages or disadvantages of the following two C code snippets? I have an int x
which I need to access from a few functions. Is it better to use it as a reference parameter (Snippet 1) or as a global variable (Snippet 2)?
1st snippet:
struct A {
int x;
};
void init( struct A * a ) {
a->x = 0;
}
void incx( struct A * a ) {
a->x++;
}
int main(void) {
struct A a;
init(&a);
incx(&a);
return 0;
}
2nd snippet:
int x;
void init() {
x = 0;
}
void incx() {
x++;
}
int main(void) {
init();
incx();
return 0;
}