Skip to content

Instantly share code, notes, and snippets.

@khakimov
Created October 4, 2012 06:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khakimov/3831753 to your computer and use it in GitHub Desktop.
Save khakimov/3831753 to your computer and use it in GitHub Desktop.
pass struct by reference
#include <stdio.h>
struct randal {
int x;
int y;
char z;
};
void func1(int test, struct randal *p)
{
int buf[100];
buf[0] = test;
buf[1] = 1;
buf[2] = 123;
buf[3] = 5321;
buf[4] = 777;
// magic with struct
// remember that in the end, it’s all just memory
// re-read!
*p = *((struct randal *)buf);
fprintf(stderr, "in func %d\n", p->x);
}
void func2(int *om)
{
int x = 10;
*om = x;
}
int main()
{
struct randal am;
func1(123, &am);
fprintf(stderr, "%d\n", am.x);
fprintf(stderr, "=======\n");
int om;
func2(&om);
fprintf(stderr, "func2 %d\n", om);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment