Created
October 4, 2012 06:17
-
-
Save khakimov/3831753 to your computer and use it in GitHub Desktop.
pass struct by reference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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